Django 路径在使用表单后不重置



在一个名为games/vote的页面上,我有一个使用路径"add_title/"作为其操作的表单:

<form method="post" action="add_title/" method="post">

我从关联的视图中返回以下内容:

return render_to_response('games/votes.html', {'vote_list': not_owned_vote_list,},
                        context_instance = RequestContext(request))

然后,从视图返回后,URL 将保留在游戏/投票/add_title。

我尝试更改请求的路径和path_info属性,但无济于事:

request.path = "/games/vote/"
request.path_info = "/games/vote/"

我希望路径在返回网页时为/games/vote。

我做错了什么?

你不能像那样改变路径。唯一的方法是告诉浏览器重定向到不同的URL - 事实上,这正是文档在表单 POST 后建议您做的事情。

if form.is_valid():
    ... process ...
    return HttpResponseRedirect('/games/vote/')

(此外,您还应该考虑使用命名 URL 和reverse(),而不是对 URL 进行硬编码。

最新更新