Django template, if tag based on current URL value

后端 未结 2 999
清歌不尽
清歌不尽 2020-12-05 23:23

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, wi

相关标签:
2条回答
  • 2020-12-05 23:45

    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.

    0 讨论(0)
  • 2020-12-06 00:06

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

    {% if request.get_full_path == "/account/login/" %}
    
    0 讨论(0)
提交回复
热议问题