Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

python - Passing variable urlname to url tag in django template

What I'd like to do (for a recent changes 'widget' - not a django widget in this case) is pass a urlname into my template as a variable, then use it like so: {% url sitechangeobject.urlname %} Where urlname is a string containing a valid name for a url.

Is this possible? The template keeps breaking saying it can't find sitechangeobject.urlname as a name (which is quite right, it doesn't exist). Is there any way to make it look inside that variable?

There are other ways to solve this problem if not, just thought I'd check though.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As of Django 1.3 the {% url %} tag properly supports:

{% url view_name_variable %}
{% url 'view_name_string' %}

...this becomes the default behaviour in Django 1.5.

Previously, you had only the option to do this:

{% url view_name_string %}

To get the tag to work in this way in Django 1.3 and 1.4 projects, you will need the following line to the top of every template you use it in:

{% load url from future %}

According to the Django 1.3 release notes:

...in Django 1.5, the old behavior will be replaced with the new behavior. To ensure compatibility with future versions of Django, existing templates should be modified to use the new future libraries and syntax.

Note that support for {% load url from future %} has been removed in Django 1.9.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...