Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django HttpResponseRedirect

Tags:

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.

like image 902
David542 Avatar asked Apr 28 '11 18:04

David542


People also ask

What is HttpResponseRedirect in Django?

HttpResponseRedirect takes a single argument: the URL to which the user will be redirected (see the following point for how we construct the URL in this case). As the Python comment above points out, you should always return an HttpResponseRedirect after successfully dealing with POST data.

What is the difference between HttpResponseRedirect and redirect in Django?

There is a difference between the two: In the case of HttpResponseRedirect the first argument can only be a url . redirect which will ultimately return a HttpResponseRedirect can accept a model , view , or url as it's "to" argument. So it is a little more flexible in what it can "redirect" to.

How do I redirect a specific page in Django?

Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .

What does reverse () do in Django?

the reverse function allows to retrieve url details from url's.py file through the name value provided there. This is the major use of reverse function in Django. The redirect variable is the variable here which will have the reversed value. So the reversed url value will be placed here.


2 Answers

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.

like image 69
vad Avatar answered Sep 23 '22 20:09

vad


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.

like image 45
alireza sadeghpour Avatar answered Sep 25 '22 20:09

alireza sadeghpour