How can I get the reverse url for a Django Flatpages template
Write your base urls conf to point to your flatpages. Assume it is under pages:
urlpatterns = patterns('',
...
url(r'^pages/', include('project.pages.urls')),
...
)
Then write your flatpages as normal:
urlpatterns = patterns('django.views.generic.simple',
url(regex=r'^resume/$', view='direct_to_template', kwargs={'template': 'resume.html'}, name='resume'),
url(regex=r'^about/$', view='direct_to_template', kwargs={'template': 'about.html'}, name='about'),
url(regex=r'^books/$', view='direct_to_template', kwargs={'template': 'library.html'},name='books'),
)
Then your template just refers to them in the usual fashion:
<div id="pages">
...
<div class="pagelinks">
<a href="{% url about %}">ABOUT</a>
</div>
</div>
You need to redeclare the url conf and cannot rely on the official 'django.contrib.flatpages.urls'
that the doc is encouraging us to use.
This won't be more difficult, just include in your urls.py
from django.conf.urls import patterns, url
urlpatterns += patterns('',
...
url(r'^pages(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage', name='flatpage'),
...
)
And now you can use your usual reverse url template tag
<a href='{% url 'flatpage' url="/about-us/" %}'>About Us</a>
Or to display a list of all flat pages
<ul>
{% get_flatpages as flatpages %}
{% for page in flatpages %}
<li><a href="{% url 'flatpage' url=page.url %}">{{ page.title }}</a></li>
{% endfor %}
</ul>
None of the solutions mentioned sufficiently followed the DRY principle in my opinion, so I just did this:
# core/templatetags/hacks.py
from django import template
register = template.Library()
@register.simple_tag
def flaturl(title):
"""
Returns the url for a flatpage based on the title.
NOTE: Obviously the title must be unique.
"""
from django.contrib.flatpages.models import FlatPage
try:
page = FlatPage.objects.get(title=title)
except:
return ""
return page.url
Then in any template that needs to make a link, I did this:
{% load hacks %}
...
<a href="{% flaturl 'My Page Title' %}">Page Title</a>
I might add some caching in there to keep the performance up, but this works for me.
I thought the advantage of Flatpages was you didn't have to create any view stubs or url confs? It's a bit pointless otherwise... if you're creating views and urls you may as well save the flatpage content as template html instead.
try this instead: https://github.com/0sn/nameremoved/wiki/flatpages
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 %}
<ul>
{% for page in get_flatpages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
Personally, I rarely reference flatpages outside of website's main menu, which is included via {% include 'includes/nav.html' %}
and looks like this:
<ul>
<li><a href="/about/">About</a></li>
<li><a href="/credits/">Credits</a></li>
...
</ul>
I don't feel I violate any DRY KISS or something:)
proper Django>= 1.10:
urls.py
urlpatterns += [
url(r'^(?P<url>.*/)$', flatpage, name='flatpage'),
]
easy lookup inside the template:
{% url "flatpage" url="SOME_URL" %}
where SOME_URL is the value frome the flatpage.url
field