0

这是我的 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')
)

这是我的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'

我尝试使用:

 return HttpResponseRedirect('success') 

或者

 return HttpResponseRedirect(reverse('success'))

但它无法呈现success.html。谁能告诉我为什么?非常感谢 !

4

1 回答 1

1

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.

于 2013-10-08T09:59:31.790 回答