我正在使用Django's comments framework。每当有人发表评论时,他都会被重定向到成功页面(posted.html
)。我不希望成功页面显示出来。我只想重新加载当前页面(使用新的评论)。如何停止重定向?
答案 0 :(得分:5)
添加名为next
的隐藏表单字段是可行的方法,但您应该使用request.get_full_path
,因为request.path
不包含查询字符串:
<input type="hidden" name="next" value="{{ request.get_full_path }}" />
答案 1 :(得分:1)
通过查看:contrib.comments.views.comments中的来源,看起来您可以提供“下一个”参数来覆盖重定向所在的位置。
#django.contrib.comments.views.comments
@csrf_protect
@require_POST
def post_comment(request, next=None, using=None):
#more code here...
# Check to see if the POST data overrides the view's next argument.
next = data.get("next", next)
#more code here...
我会尝试在评论表单中添加一个隐藏字段,其名称为“next”,并且是您当前网址的值。如果这不起作用,您可能必须提供自己的视图和网址。希望有用!