Atma
Atma

Reputation: 29767

django redirect not redirecting

I have the following form logic in my view:

if request.method == 'POST':
        form = MyForm(request.POST, request.FILES)
        if form.is_valid():

            my_form = form.save()                                          )
            print 'before redirect'
            redirect('customer:department-edit')
            print 'after redirect'

My url entry looks like this:

url(r'^departments/$', views.departments_view, name='department-edit'),

I get the following output:

before redirect
after redirect

Why would the redirect not occur after the form is submitted?

Upvotes: 0

Views: 186

Answers (1)

achedeuzot
achedeuzot

Reputation: 4384

It seems you forgot to add a return statement before redirect().

Why is there a need for a return ? Because the redirect method is just a shortcut to a HttpResponseRedirect, so it behaves like any other action: it has to return a response.

So your code should look like this:

...
print 'before redirect'
return redirect('customer:department-edit')
print 'after redirect'
...

See The Django Documentation example :)

Upvotes: 5

Related Questions