Commit 2665861b authored by Давид Ли's avatar Давид Ли

lesson 57

parent 7e1bde5a
No preview for this file type
...@@ -35,11 +35,9 @@ class ArticleModelForm(forms.ModelForm): ...@@ -35,11 +35,9 @@ class ArticleModelForm(forms.ModelForm):
class CommentModelForm(forms.ModelForm): class CommentModelForm(forms.ModelForm):
class Meta: class Meta:
model = Comment model = Comment
fields = ['author', 'text', 'article'] fields = ['text']
widgets = { widgets = {
'text': forms.TextInput(attrs={'class': 'form-control mb-3'}), 'text': forms.TextInput(attrs={'class': 'form-control mb-3'}),
'author': forms.HiddenInput(attrs={'class': 'form-control mb-3'}),
'article': forms.HiddenInput()
} }
......
...@@ -40,8 +40,8 @@ ...@@ -40,8 +40,8 @@
<form action="{% url 'comment_create' %}" method="POST"> <form action="{% url 'comment_create' %}" method="POST">
{% csrf_token %} {% csrf_token %}
{% include 'partial/form.html' with button_text='add' %} {% include 'partial/form.html' with button_text='add' %}
<input type="hidden" name="article" value="{{ article.id }}"> {# <input type="hidden" name="article" value="{{ article.id }}">#}
<input type="hidden" name="author" value="{{ request.user.id }}"> {# <input type="hidden" name="author" value="{{ request.user.id }}">#}
</form> </form>
{% for comment in comments %} {% for comment in comments %}
......
...@@ -11,8 +11,6 @@ ...@@ -11,8 +11,6 @@
</li> </li>
{% endif %} {% endif %}
{% for num in paginator.page_range %} {% for num in paginator.page_range %}
<li class=" <li class="
page-item page-item
......
...@@ -25,6 +25,7 @@ class ArticleIndexView(ListView): ...@@ -25,6 +25,7 @@ class ArticleIndexView(ListView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.form = self.get_search_form() self.form = self.get_search_form()
self.search_value = self.get_search_value() self.search_value = self.get_search_value()
session = request.session
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get_context_data(self, *, object_list=None, **kwargs): def get_context_data(self, *, object_list=None, **kwargs):
...@@ -63,7 +64,7 @@ class ArticleCreateView(CreateView): ...@@ -63,7 +64,7 @@ class ArticleCreateView(CreateView):
return reverse('article_detail', kwargs={'id': self.object.id}) return reverse('article_detail', kwargs={'id': self.object.id})
class ArticleDetailView(DetailView): class ArticleDetailView(LoginRequiredMixin, DetailView):
raise_exception = True raise_exception = True
template_name = 'article/article_detail.html' template_name = 'article/article_detail.html'
model = Article model = Article
...@@ -71,6 +72,11 @@ class ArticleDetailView(DetailView): ...@@ -71,6 +72,11 @@ class ArticleDetailView(DetailView):
pk_url_kwarg = 'id' pk_url_kwarg = 'id'
extra_context = {'form': CommentModelForm} extra_context = {'form': CommentModelForm}
def dispatch(self, request, *args, **kwargs):
request.session['article_id'] = kwargs.get('id')
request.session['author_id'] = request.user.id
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
return super().get_context_data( return super().get_context_data(
comments=self.object.comments.order_by('-created_at'), comments=self.object.comments.order_by('-created_at'),
......
...@@ -14,6 +14,11 @@ class CommentCreateView(CreateView): ...@@ -14,6 +14,11 @@ class CommentCreateView(CreateView):
template_name = 'comment/create_update.html' template_name = 'comment/create_update.html'
form_class = CommentModelForm form_class = CommentModelForm
def form_valid(self, form):
form.instance.author_id = self.request.session.get('author_id')
form.instance.article_id = self.request.session.get('article_id')
return super().form_valid(form)
def get_success_url(self): def get_success_url(self):
return reverse('article_detail', kwargs={'id': self.object.article.id}) return reverse('article_detail', kwargs={'id': self.object.article.id})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment