Search code examples
djangodjango-templatesdjango-2.0

Django if logic in template


Is something like this possible in a template?

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

If not, how can I achieve this result being True? I want to this to be true when user is looking at a specific post such as post/1.


Solution

  • Yes, you can using the as var syntax:

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