Edgar
Edgar

Reputation: 101

how to reverse url in django template syntax

for example, I can reverse a url in this way:

{% url 'home:index' %}

but if I need to compare a url in a if sentence, like this:

{% if request.path == url %}

and I want to replace the url with a reverse one but I can't do this:

{% if request.path == {% url 'home:index' %} %}

So is there another way can solve this?

Thanks a lot !

Upvotes: 1

Views: 2438

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32274

The url tag takes an argument as <var> that saves the result of the reverse to a variable. You can then use this variable in your comparison

{% url 'home:index' as home_url %}
{% if request.path == home_url %}

Upvotes: 2

Related Questions