Django RedirectView and reverse() doesn't work together?

前端 未结 4 647
生来不讨喜
生来不讨喜 2021-01-11 12:49

I\'m having this weird problem.

When I did this:

from django.core.urlresolvers import reverse
reverse(\'account-reco-about-you\')
# returns \'/accoun         


        
4条回答
  •  暖寄归人
    2021-01-11 13:17

    You need to use "reverse_lazy" that is defined in "django.core.urlresolvers" in Django 1.4 and above.

    Here is an example urls.py:

    from django.conf.urls import patterns, include, url
    from django.views.generic import RedirectView
    from django.core.urlresolvers import reverse_lazy
    
    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('apps.website.views',
        url(r'^$', 'home', name='website_home'),
        url(r'^redirect-home/$', RedirectView.as_view(url=reverse_lazy('website_home')), 
            name='redirect_home'),
    
    )
    

    So in the above example, the url "/redirect-home" will redirect to "/". Hope this helps.

提交回复
热议问题