24👍
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>
25👍
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
[...]
<a href="{% url about %}"><span>{% trans "About us" %}</span></a>
<a href="{% url license %}"><span>{% trans "Licensing" %}</span></a>
[...]
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.
- [Django]-Django – Simple custom template tag example
- [Django]-How to set a value of a variable inside a template code?
- [Django]-How do I serve a text file from Django?
2👍
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>
- [Django]-UnicodeDecodeError : 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
- [Django]-How to run django unit-tests on production database?
- [Django]-Passing variable urlname to url tag in django template
2👍
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
- [Django]-Anyone using Django in the "Enterprise"
- [Django]-Django rest framework – self.context doesn't have request attribute
- [Django]-Update/append serializer.data in Django Rest Framework
2👍
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.
- [Django]-Deploying Google Analytics With Django
- [Django]-Using related_name correctly in Django
- [Django]-Manually logging in a user without password
2👍
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:)
- [Django]-Django Model set foreign key to a field of another Model
- [Django]-Resource 'corpora/wordnet' not found on Heroku
- [Django]-Django – deterministic=True requires SQLite 3.8.3 or higher upon running python manage.py runserver
1👍
When you create any flatpage, you need to specify an URL which is saved as part of the model. Hence you can retrieve the URL from any flatpage object. In a template:
{{ flatpage.url }}
Remapping flatpage URLs in urls.py
and then having to use reverse sort of defeats the purpose of the flatpages app.
- [Django]-Identify which submit button was clicked in Django form submit
- [Django]-Django DB Settings 'Improperly Configured' Error
- [Django]-Displaying a Table in Django from Database
1👍
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>
- [Django]-Manage.py runserver
- [Django]-Python Requests POST doing a GET?
- [Django]-Django 1.7 – How do I suppress "(1_6.W001) Some project unittests may not execute as expected."?
1👍
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
- [Django]-How to compare dates in Django templates
- [Django]-Django project base template
- [Django]-Filter ManyToMany box in Django Admin
0👍
According to this django documentation for flatpages
You can simply do
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
In your template.
- [Django]-Elegantly handle site-specific settings/configuration in svn/hg/git/etc?
- [Django]-Django – show the length of a queryset in a template
- [Django]-403 Forbidden error with Django and mod_wsgi