I want add comment to post on his page (“post detail” page).
I was find answer, but it create comment on other page. I want create comment on page of “post detail”.
urls.py
JavaScript
x
2
1
url(r'^post/(?P<pk>d+)/create/$', views.CommentCreate.as_view(), name='comment_create'),
2
models.py
JavaScript
1
12
12
1
class Comment(models.Model):
2
description = RichTextUploadingField()
3
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
4
comment_date = models.DateTimeField(auto_now_add=True, null=True)
5
post = models.ForeignKey(Post, on_delete=models.CASCADE)
6
7
class Meta:
8
ordering = ["-comment_date"]
9
10
def __str__(self):
11
return "{}".format(self.description)
12
forms.py
JavaScript
1
5
1
class CommentForm(forms.ModelForm):
2
class Meta:
3
model = Comment
4
fields = ['description']
5
views.py
JavaScript
1
21
21
1
class PostDetailView(generic.DetailView):
2
model = Post
3
4
5
class CommentCreate(LoginRequiredMixin, CreateView):
6
model = Comment
7
fields = ['description']
8
9
def get_context_data(self, **kwargs):
10
context = super(CommentCreate, self).get_context_data(**kwargs)
11
context['post'] = get_object_or_404(Post, pk = self.kwargs['pk'])
12
return context
13
14
def form_valid(self, form):
15
form.instance.author = self.request.user
16
form.instance.post=get_object_or_404(Post, pk = self.kwargs['pk'])
17
return super(CommentCreate, self).form_valid(form)
18
19
def get_success_url(self):
20
return reverse('post-detail', kwargs={'pk': self.kwargs['pk'],})
21
comment_form.html
JavaScript
1
10
10
1
2
<form action="" method="post">
3
{% csrf_token %}
4
<table>
5
{{ form.as_table }}
6
</table>
7
<button type="submit">Submit</button>
8
</form>
9
10
post_detail.html
JavaScript
1
9
1
2
{% for comment in post.comment_set.all %}
3
<p>{{ comment.author }} ({{ comment.comment_date }}) {{ comment.description|safe }}</p>
4
{% endfor %}
5
<hr>
6
{% if user.is_authenticated %}
7
<p><a href = "{% url 'comment_create' post.id %}">Add a new comment</a></p>
8
9
I think, “comment_form” need to redirect to “post_detail”, not generate new page for comment form.
And сould you tell, which parameters has a RichTextField (CKE), how change width, height field only in comment?
Advertisement
Answer
If you want the comment form right in your detail page then all you have to do is to add the form and post function in your View,
JavaScript
1
20
20
1
class PostDetailView(DetailView):
2
model = Post
3
template_name = 'yourdetailpage.html'
4
5
def get_context_data(self, **kwargs):
6
context = super(PostDetailView, self).get_context_data(**kwargs)
7
context['commentform'] = CommentForm()
8
return context
9
10
def post(self, request, pk):
11
post = get_object_or_404(Post, pk=pk)
12
form = CommentForm(request.POST)
13
14
if form.is_valid():
15
obj = form.save(commit=False)
16
obj.post = post
17
obj.author = self.request.user
18
obj.save()
19
return redirect('detail', post.pk)
20
And now you can add your form in your html. And add a submit button to post comment.