Django if 模板中的逻辑

Django if logic in template

在模板中可以实现这样的功能吗?

{% if request.path == url 'posts:post_detail' pk=instance.id %}

如果不是,我怎样才能达到这个结果 True?当用户查看特定 post 例如 post/1.

时,我希望这是真的

是的,您可以使用 as var 语法:

{% url 'posts:post_detail' pk=instance.id as the_url %}
...
{% if request.path == the_url %}
...do something
{% endif %}