Reversible, named URLs for Django flatpages (or multilingual-ng)?
Is there a way to have reversible, named URLs for Django flatpages (or multilingual-ng, which is flatpages + t开发者_如何学Pythonranslations)?
If not, is there a similar app available which can have named URLs for pages which are editable through the admin? (And I'd rather avoid any behemoth CMSs, please.)
edit: Please see the comments in the answers below for more discussion and clarification of the question.
As the very last thing in your root URLConf, put
(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'),
Now you can use the {% url %}
tag, with the view name flatpage
and the keyword argument url
. You can also use the reverse()
function in views.
{% url %} template tag works with URLconf, whereas flatpages are handling nonexistent urls.
If you really want to reverse flatpages into URLs, my guess is that you have to write your own {% flatpage_url %} tag, which would looks like this:
@register.simple_tag
def flatpage_url(self, name):
return FlatPage.objects.get({param}=name).url
-- where {param} is one of FlatPage model fields.
Also, you can merge {% url %} and {% flatpageurl %} tags together, so that the latter fallbacks to reverse() in case FlatPage is not found (and reverse() is what {% url %} uses).
EDIT:
I don't accept any more upvotes as James Bennet's answer is The One for this question (and I'm ashamed I overlooked such trivial solution) - so please upvote it.Aren't the proposed solutions basically the same as hardcoding the URL in the template? What's the difference between hardcoding the URL in href="URL" vs. doing it in {% url URL %}?
I think a better solution is found on: How can I get the reverse url for a Django Flatpages template, proposed by bufh.
I think the optimal way of achieving this would be adding an extra 'name' field on the FlatPage model.
精彩评论