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

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

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

10条回答
  •  感动是毒
    2021-01-31 10:54

    I agree with Anentropic that there is no point in using Django Flatpages if you need to write urlconfs to employ them. It's much more straightforward to use generic views such as TemplateView directly:

    from django.conf.urls import patterns, url
    from django.views.generic import TemplateView
    
    urlpatterns = patterns('',
        url(r'^about/$', TemplateView.as_view(template_name="about.html"), name="about"),
    )
    

    Flatpages take advantage of FlatpageFallbackMiddleware, which catches 404 errors and tries to find content for requested url in your database. The major advantage is that you don't have to touch your templates directly each time you have to change something in them, the downside is the need to use a database :)

    If you still choose to use Flatpages app, you'd better use get_flatpages template tag:

    {% load flatpages %}
    
    

    Personally, I rarely reference flatpages outside of website's main menu, which is included via {% include 'includes/nav.html' %} and looks like this:

    
    

    I don't feel I violate any DRY KISS or something:)

提交回复
热议问题