Django HttpResponseRedirect
Solution 1
It's not the POST button that should redirect, but the view.
If not differently specified, the form (the HTML form tag) POSTs to the same URL. If the form is on /contact/, it POSTs on /contact/ (with or without slash, it's the same).
It's in the view that you should redirect to thanks. From the doc:
def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ContactForm() # An unbound form
return render_to_response('contact.html', {
'form': form,
})
Change /thanks/
to /contact/thanks/
and you're done.
Solution 2
All of the responses are correct but a better approach is to give names to your URLs in urls.py
and hint to them in views with reverse function (instead of hard coding URL in views).
urls.py:
(r'^contact/$', contact, name='contact'),
(r'^contact/thanks/$', contact_thanks, name='thanks'),
And hint them in views.py like this:
from django.urls import reverse
return HttpResponseRedirect(reverse('app_name:thanks'))
This is better for future approach and follow the DRY principle of Django.
Solution 3
Just try this. It worked for me.
return HttpResponseRedirect('thanks/')
Note:- Remove the forward slash before
David542
Updated on May 20, 2020Comments
-
David542 about 4 years
I have created a basic contact form, and when the user submits information, it should redirect to the "Thank You" page.
views.py:
def contact(request): # if no errors... return HttpResponseRedirect('/thanks/')
urls.py:
(r'^contact/$', contact), (r'^contact/thanks/$', contact_thanks),
Both pages work at the hard-coded URL. However, when I submit the form on
/contact/
it redirects to/contact
(no ending slash), which is a nonexistent page (either a 404 or an error page telling me I need a slash).What is the reason it not correctly redirecting, and how can I fix this?
UPDATE: the
return HttpResponseRedirect('/contact/thanks/')
is what I now have, but the problem is that the submit button (using POST) does not redirect to the URL -- it doesn't redirect at all. -
Admin about 13 yearsIsn't the setting True by default? - It is.
-
aviraldg about 13 years@David542: It might not be the OP's project, or he might have changed it unintentionally.
-
MegaBytes about 9 yearsHow can we send the data using HttpResponseRedirect?
-
vad about 9 years@MegaBytes: data? what do you mean?
-
MegaBytes about 9 yearsThe data means any information, from one view to another view. e.g. Can We do this way, data = {'xyz': 'abcd', 'ddd':'yyyy'} return HttpResponseRedirect('/contact/thanks/', data)
. I am new in django, and I reading doc also but I am not getting a way to do it.
render` is one way but I dont want to go to any browser page. just transfer data between views. -
vjimw over 7 yearsI know this is an old comment, but yes you can send data via the querystring along the lines of this: redirect_url = '{redirect_url}?{querystring}'.format( redirect_url=reverse("YOUR_VIEW_NAME", kwargs={'named_keywork': obj.pk}), querystring=urlencode( {'xyz': 'abcd', 'ddd': 'yyyy'}, doseq=True ) )
-
commadelimited almost 7 yearsReverse is great for smaller applications, but can lead to high overhead when working with applications that have large numbers of routes. For example at my company we have a URLs.py file with hundreds of routes, and sub applications that have even more. I'd say all total we have 400-500 or more? Just be mindful of that lookup hit.
-
Mohammed Shareef C almost 6 years@commadelimited If there are a lot of urls, then
reverse
is overhead. I think hardcoding oif url will cause a bigger overhead for developer later if we change the url -
Dave almost 6 yearsYou'll need this import:
from django.urls import reverse
-
dina over 5 yearswe can also use reverse function with django 2.1 (not sure about older versions). Example:
HttpResponseRedirect(reverse('view_name'))
, where view name is specified inurls.py
-
Vishal Patel about 2 yearswhat happen if user will not wait for message and go to the another page. still message display with redirect to contact page or getting error?