How can I get the reverse url for a Django Flatpages template

后端 未结 10 1161
情深已故
情深已故 2021-01-31 10:33

How can I get the reverse url for a Django Flatpages template

10条回答
  •  迷失自我
    2021-01-31 11:06

    I prefer the following solution (require Django >= 1.0).

    settings.py

    INSTALLED_APPS+= ('django.contrib.flatpages',)
    

    urls.py

    urlpatterns+= patterns('django.contrib.flatpages.views',
        url(r'^about-us/$', 'flatpage', {'url': '/about-us/'}, name='about'),
        url(r'^license/$', 'flatpage', {'url': '/license/'}, name='license'),
    )
    

    In your templates

    [...]
    {% trans "About us" %}
    {% trans "Licensing" %}
    [...]
    

    Or in your code

    from django.core.urlresolvers import reverse
    [...]
    reverse('license')
    [...]
    

    That way you don't need to use django.contrib.flatpages.middleware.FlatpageFallbackMiddleware and the reverse works as usual without writing so much code as in the other solutions.

    Cheers.

提交回复
热议问题