[Fixed]-Assign Value of Named URL to a Variable in Django Templates

52👍

Why create a new template tag/filter if the feature is in core?

Look the samples at: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url

{% url 'path.to.view' arg arg2 as the_url %}

<a href="{{ the_url }}">I'm linking to {{ the_url }}</a>

and

{% url 'path.to.view' as the_url %}
{% if the_url %}
  <a href="{{ the_url }}">Link to optional stuff</a>
{% endif %}
👤Darwin

-4👍

I think you’ll have to either create your own template tag to manage this problem or create the necessary data in the view and pass it in to the template.

Depending on exactly what you’re trying to do maybe including another template and sending in your defined variable could do it, but I doubt it.

The thinking behind Djangos templating system is to make it so there isn’t very much logic in the templates. And thus it’s back to either preparing the data you need for output in the view or making a template tag.

👤gaqzi

Leave a comment