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

lesson 57

parent 7e1bde5a
No preview for this file type
......@@ -35,11 +35,9 @@ class ArticleModelForm(forms.ModelForm):
class CommentModelForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['author', 'text', 'article']
fields = ['text']
widgets = {
'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 @@
<form action="{% url 'comment_create' %}" method="POST">
{% csrf_token %}
{% include 'partial/form.html' with button_text='add' %}
<input type="hidden" name="article" value="{{ article.id }}">
<input type="hidden" name="author" value="{{ request.user.id }}">
{# <input type="hidden" name="article" value="{{ article.id }}">#}
{# <input type="hidden" name="author" value="{{ request.user.id }}">#}
</form>
{% for comment in comments %}
......
......@@ -11,8 +11,6 @@
</li>
{% endif %}
{% for num in paginator.page_range %}
<li class="
page-item
......
......@@ -25,6 +25,7 @@ class ArticleIndexView(ListView):
def dispatch(self, request, *args, **kwargs):
self.form = self.get_search_form()
self.search_value = self.get_search_value()
session = request.session
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, *, object_list=None, **kwargs):
......@@ -63,7 +64,7 @@ class ArticleCreateView(CreateView):
return reverse('article_detail', kwargs={'id': self.object.id})
class ArticleDetailView(DetailView):
class ArticleDetailView(LoginRequiredMixin, DetailView):
raise_exception = True
template_name = 'article/article_detail.html'
model = Article
......@@ -71,6 +72,11 @@ class ArticleDetailView(DetailView):
pk_url_kwarg = 'id'
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):
return super().get_context_data(
comments=self.object.comments.order_by('-created_at'),
......
......@@ -14,6 +14,11 @@ class CommentCreateView(CreateView):
template_name = 'comment/create_update.html'
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):
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