在Django模板中实现正则表达式 [英] Implement regex in django template
本文介绍了在Django模板中实现正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过检查url是否与当前url相匹配来激活页面导航。
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>
现在我正在使用分页,当转到不同的页面时,URL如下所示:
Now I am using pagination, which when going to different page results in url being like this:
从 / blog /
到
/blog/?page=2
/blog/?page=3
上面的代码不起作用。因此,无论如何都可以在模板中使用正则表达式来获取任何内容(例如 / blog / *
)并将其激活。非常感谢您的帮助和指导。谢谢。
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.
推荐答案
您可以做
{% if '/blog/' in request.path %}class="active"{% endif %}
不需要正则表达式
这篇关于在Django模板中实现正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文