评论Django的帖子

时间:2017-04-04 10:35:59

标签: python django

我希望用户对帖子发表评论,现在用户需要重定向到另一个页面评论“add_comment_to_post.html”,并且在此行提交表单时仍然会出现错误'return redirect('blog:index' ,post.pk)' 'NoReverseMatch at / blog / post / 70 / comment /'

views.py

class IndexView(generic.ListView):
        template_name = 'blog/index.html'
        context_object_name = 'posts'
        def get_queryset(self):
            return Post.objects.all()



def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
    form = CommentForm(request.POST)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.post = post
        comment.save()
        return redirect('blog:index', post.pk)
else:
    form = CommentForm()
return render(request, 'blog/add_comment_to_post.html', {'form': form})

这里是'blog / index.html'

中的表单
<form enctype="multipart/form-data" method="post" class="post-form">
        {% csrf_token %}
        {{ form.textc }}
        <button type="submit" class="save btn btn-default">Send</button>
    </form>

form.py

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('textc',)

urls.py

url(r'^post/(?P<pk>\d+)/comment/$', views.add_comment_to_post, name='add_comment_to_post'),

0 个答案:

没有答案