changzhi
changzhi

Reputation: 2709

Django HttpResponseRedirect can not found url

This is my urls.py:

urlpatterns = patterns('horizon.views',
url(r'home/$', 'user_home', name='user_home'),
url(r'register/$', Register.as_view(), name='register'),
url(r'success/$', Success.as_view(), name='success')
)

And this my views.py:

 class Register(forms.ModalFormView):
    template_name = 'auth/test.html'
    form_class = CreateUser
    success_url = reverse_lazy('login')

 class Success(generic.TemplateView):
     template_name = 'auth/success.html'

I try to use:

 return HttpResponseRedirect('success') 

or

 return HttpResponseRedirect(reverse('success'))

but it is can not render the success.html. Who can tell me why ? Thanks a lot !

Upvotes: 0

Views: 467

Answers (1)

Ewan
Ewan

Reputation: 15058

Please post the exact error you are getting.

When doing your HttpResponseRedirect it's best not to hardcode URLs like '/success/'.

Instead do this:

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect


....
    # Here 'success' is the URL name you have given.
    return HttpResponseRedirect(reverse('success'))

This way, if you change the success URL (not that you should..) it will automatically update throughout your app rather than having to go back and change all your hardcoded values.

Read more about it in the Django Reverse resolution of URLs documentation.

Upvotes: 1

Related Questions