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

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

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

10条回答
  •  猫巷女王i
    2021-01-31 11:04

    Include flatpages in your root urlconf:

    from django.conf.urls.defaults import *
    
    urlpatterns = patterns('',
        ('^pages/', include('django.contrib.flatpages.urls')),
    )
    

    Then, in your view you can call reverse like so:

    from django.core.urlresolvers import reverse
    
    reverse('django.contrib.flatpages.views.flatpage', kwargs={'url': '/about-us/'})
    # Gives: /pages/about-us/
    

    In templates, use the {% url %} tag (which calls reverse internally):

    About Us
    

提交回复
热议问题