【发布时间】:2017-02-28 05:18:53
【问题描述】:
在一个简单的博客应用程序中,当用户在一篇文章中访问时,推荐的方法是重定向到相同的可变路由或永久链接以显示新评论(在 django 中)?
urlpatterns = [
...
url(r'^comments/(?P<post_id>[0-9]+)$', views.comments, name="thread"),
url(r'^post/comment/$', views.post_comment, name="post_comment"),
]
在视图中,我可以使用request.get_full_path() 获取网址,但我认为与其剥离post_id,还有更好的方法将其传递给重定向。示例视图(但不正确):
def post_comment(request):
author = User.objects.get(user=request.user)
new_comment = request.POST.get('commentContent', None)
parent_object = None
comment = Comment.create(author=author,
new_comment=new_comment,
parent=parent_object)
comment.save()
return redirect('/comments/{}'.format(comment.post.id))
提交评论表单将记录:
[28/Feb/2017 05:21:33] "POST /post/comment/ HTTP/1.1" 302 0
[28/Feb/2017 05:21:33] "GET /comments/{{post_id}} HTTP/1.1" 200 44831
表单正确发布,但页面不会重新加载/重定向
谢谢
【问题讨论】:
标签: python django django-forms