djangoreversedjango-flatpages

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


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


Solution

  • 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):

    <a href='{% url django.contrib.flatpages.views.flatpage url="/about-us/" %}'>About Us</a>