mymen
mymen

Reputation: 85

Django froms submit action and HttpResponseRedirect difference

Referring from the form topic of django documentation, if in a view function I write,

if form.is_valid():
    return HttpResponseRedirect('/thanks/')

And in the form template I give the action like,

<form action="/your-name/" method="post">

Then on submit the form will go to the view mapped tho /your-name/ url in urls.py but what about the HttpResponseRedirect('/thanks/') line in the function from where I rendering the form? If a form is valid then I save the form. but what will be the url in the action of the form. now def get_name(request): is the function mapped to /user/ url. I hope you understand my confusion here. need some help.

Upvotes: 0

Views: 271

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

This is an exemple of the "post redirect get" pattern - if a post is succesfull (the form was valid and the processing went ok), it's good practice to return a redirect, which will be followed by a get from the user-agent on the url you redirected to. It avoids multiple submissions if the user try to reload the page.

Where you redirect to is up to you - you can just redirect to the same view (usually displaying a success message to the user), or redirect to another view.

As a side note: hardcoding urls is a bad practice in Django, you should use the url reverse feature instead.

Upvotes: 2

Related Questions