Alison
Alison

Reputation: 5840

Django path not resetting after use of form

On a page called games/vote I have a form that uses the path "add_title/" as its action:

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

I return the following from the associated view:

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

The url then remains at games/vote/add_title upon return from the view.

I tried changing the path and path_info attributes of the request but to no avail:

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

I want the path to be /games/vote upon return to the web page.

What am I doing wrong?

Upvotes: 2

Views: 85

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599778

You can't change the path like that. The only way to do it is to tell the browser to redirect to a different URL - which, in fact, is exactly the thing you are recommended to do by the docs after a form POST.

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

(Also you should look at using named URLs and reverse() rather than hard-coding the URLs.)

Upvotes: 1

Related Questions