我试着把评论写在页面上,然后改为在页面上重定向我有一个空窗口在浏览器和
[b'
Not Found
The requested URL /post/add_comment/test_single_post/ was not found on this server.
']和{}在终端日志中。
我不明白为什么在这种情况下找不到页面(404)。在
视图class SinglePost(DetailView):
model = Post
template_name = 'post.html'
def get_context_data(self, **kwargs):
comment_form = CommentForm
context = super(SinglePost, self).get_context_data(**kwargs)
comments = Comments.objects.filter(comment_id=self.object).order_by('-added')
context['comments'] = comments
context['form'] = comment_form
return context
@csrf_protect
def add_comment(request, slug):
"""
Add comment to.
"""
if request.POST:
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.comment = Post.objects.get(slug=slug)
form.save()
return redirect('/post/{0}/'.format(slug))
网址
^{pr2}$
模板
Comments:
{% for comment in comments %}
{{ comment.added }} | {{ comment.author }}
{{ comment.comment_text }}
{% empty %}
There are no comments here yet. Be first :)
{% endfor %}
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.subject.errors }}
Add your name:
{{ form.author|addclass:"form-control" }}
Add your comment here:
{{ form.comment_text|addclass:'form-control comment-textarea' }}
谁能给我一个如何解决这个问题的答案吗?在