Implement regex in django template

你离开我真会死。 提交于 2019-12-08 10:24:18

问题


I am making the navigation of the page as active by checking if the url matches with the current url.

<li>
  {% url 'blog' as blog_url %}
  <a href="{{blog_url}}" {% if request.get_full_path == blog_url %}class="active"{% endif %}>Blog</a>
</li>

Now I am using pagination, which when going to different page results in url being like this:

From /blog/ to

/blog/?page=2
/blog/?page=3

And the above code does not work. So is there anyway to use regex in the template to get anything (like /blog/*) and make it as active. Your help and guidance will be very much appreciated. Thank you.


回答1:


you can just do

{% if '/blog/' in request.path %}class="active"{% endif %}

no need for regexp



来源:https://stackoverflow.com/questions/30884711/implement-regex-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!