[Django]-Django get current view in template

-3👍

Just set attribute to request object in view:

setattr(request, 'view', 'app.views.func')

and check this in template:

{% if request.view == 'app.views.func' %}
    do something
{% endif %}

It worked for me.

22👍

Type just in view

{% with request.resolver_match.view_name as view_name %}
    ...
    {{ view_name }}
    ...
{% endwith %}

1👍

I’m not sure I completely understand the requirement, but take a look at inspect.stack.

inspect.stack()[1][3]

Leave a comment