A (last-minute) compromise on {% url %}

355 views
Skip to first unread message

Jacob Kaplan-Moss

unread,
Aug 29, 2008, 11:18:49 AM8/29/08
to django-d...@googlegroups.com
Hi folks --

While working on some URL-related issues, I ran across a pretty big
problem with having ``{% url %}`` propagate ``NoReverseMatch`` up into
the templates: if it does that, there's no way to have "optional"
links. The perfect example is #7810 and the admin docs: if the URLs
for the admin doc views are installed, the admin should show a
(correct) link to the docs; if they're not installed, the link
shouldn't be there. Unfortunately, with the current ``url`` tag, there
isn't any way to make this work.

I started poking at it, and it turned out to be *very* easy to add a
``{% url ... as varname %}`` syntax to capture the URL into a variable
for later use. As a side effect, I made this syntax *not* raise errors
but instead return an empty string. This makes for a very natural
use::

{% url django-admindocs-docroot as docroot %}
{% if docroot %}
<a href="{{ docroot }}">Documentation</a>
{% endif %}

Patch here: http://code.djangoproject.com/git/?p=django;a=commitdiff;h=6c7d85a3cf6a3c7b161d960f86ea1cb111ded605

So: I know this is technically a "feature addition," but it makes the
fixes to some silly bugs far easier. Are there any objections to me
sneaking this into 1.0?

Jacob

David Larlet

unread,
Aug 29, 2008, 11:43:48 AM8/29/08
to django-d...@googlegroups.com

Le 29 août 08 à 17:18, Jacob Kaplan-Moss a écrit :

+1 for this, before I test if someone already knows the answer, is it
possible to use this var in blocktrans?

{% url args as myurl %}
{% blocktrans %}
foo <a href="{{ myurl }}">baz</a>
{% endblocktrans %}

That's potentially the best solution for the endless discussion about
urls in blocktrans.

Best,
David


Eduardo O. Padoan

unread,
Aug 29, 2008, 11:53:28 AM8/29/08
to django-d...@googlegroups.com

+1 -- Pinax could deprecate {% captureas %} with this feature. Seems
like a non-intentional pro :)

--
Eduardo de Oliveira Padoan
http://djangopeople.net/edcrypt/
http://whoisi.com/p/514
http://pinax.hotcluboffrance.com/profiles/edcrypt/

Jannis Leidel

unread,
Aug 29, 2008, 3:07:35 PM8/29/08
to django-d...@googlegroups.com

Am 29.08.2008 um 17:18 schrieb Jacob Kaplan-Moss:

> Are there any objections to me
> sneaking this into 1.0?

Hell, no! That's perfect and also works with blocktrans.

..and let's us kill that captureas in Pinax, yay!

Cheers,
Jannis

daonb

unread,
Aug 30, 2008, 11:50:12 AM8/30/08
to Django developers
+1 for a sweet feature.
Reply all
Reply to author
Forward
0 new messages