34

I want to be able to do an if tag based on the current URL value.

for example, if the current page's url is accounts/login/ then don't show a link, without passing a variable from the view.

I am not sure how I can write an {% if %} tag for this, is it possible?

bash-
  • 6,144
  • 10
  • 43
  • 51
  • possible duplicate: http://stackoverflow.com/questions/2882490/get-the-current-url-within-a-django-template – arie Sep 18 '11 at 15:38

4 Answers4

69

You can also do this for dynamic urls using:

{% url 'show_user_page' user=user as the_url %}
{% if request.get_full_path == the_url %}something{% endif %}

where your urls.py contains something like:

(r'^myapp/user/(?P<user>\d+)/$', 'show_user_page'),

I know this because I just spent ages drafting a stackoverflow question, when I found the answer in the docs.

I'd say even in simple cases this might be the better approach, because it is more loosely coupled.

Flimm
  • 136,138
  • 45
  • 251
  • 267
nimasmi
  • 3,978
  • 1
  • 25
  • 41
  • Woooow. the `as` there made my day. Using same snippet over multiple pages via include tag, and needed a way to display different links depending on what page on. Priceless approach! – KhoPhi Nov 08 '15 at 18:59
  • Thank you I really thought it would be harder than this. the url alias is useful because I wasn't sure how to compare the full path to just a root index page which has nothing discernable in the path to compare for – Akin Hwan Jan 18 '19 at 17:40
  • 1
    Also make sure that `user` is in the `context` given to the template. – Julian Espinel Mar 21 '20 at 18:17
39

If you pass the "request" object to your template, then you are able to use this:

{% if request.get_full_path == "/account/login/" %}
Riley
  • 4,122
  • 3
  • 16
  • 30
Gabriel Ross
  • 5,168
  • 1
  • 28
  • 30
2

For dynamic urls, you can also use the request.resolver_match attribute (docs):

HttpRequest.resolver_match

An instance of ResolverMatch representing the resolved URL. This attribute is only set after URL resolving took place, which means it’s available in all views but not in middleware which are executed before URL resolving takes place (you can use it in process_view() though).

The returned ResolverMatch object has many useful attributes, such as the view_name attribute, which returns the same name you would have passed to the url templatetag to generate the current url.

view_name

The name of the view that matches the URL, including the namespace if there is one.

See the docs for this and other attributes.

Applying this to the example from @nimasmi's answer, you would get:

{% if request.resolver_match.view_name == 'show_user_page' %}something{% endif %}

where your urls.py contains something like:

(r'^myapp/user/(?P<user>\d+)/$', 'show_user_page'),

Note that when you use URL namespaces, view_name will return the namespace qualified url/view name, e.g. app:urlname.

Compared to the answer by @nimasmi, this simplifies the template code a bit, by not needing the separate {% url %} tag to generate the url to compare with. This is especially true when you do not need to compare view parameters, just the view name. If you do need to compare parameters in the url, you can easily use the ResolverMatch.args and kwargs attributes, though.

Matthijs Kooijman
  • 2,498
  • 23
  • 30
0

Maybe like this? if "order" in request.path Using "in" allows you to match URLs like: customers, customer, customer/new, customer/edit, etc

            <ul class="nav nav-pills nav-fill">

                <li class="nav-item">

                    <a class="nav-link

<pre>{% if "order" in request.path %} active {% endif %} "</pre>

                        href="/orders">Order List</a>

                </li>

                <li class="nav-item">

                    <a class="nav-link

<pre>{% if "customer" in request.path %} active {% endif %} "</pre>

                        href="/customers">Customer List</a>

                </li>

                <li class="nav-item">

                    <a class="nav-link

<pre>{% if "product" in request.path %} active {% endif %} "</pre>

                       href="/products">Product List</a>

                </li>

            </ul>
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109