Dj Boris Dj Boris - 1 month ago 5x
Python Question

Django get current view in template

How can I get the full name of the current view (my_app.views.index) in a template in Django 1.5?

With forms, I have an object called "view" in the template which I read using a template tag.
But with DetailViews I doesn't see anything similar.

Is there a way using a custom template processor?



Situation 1:

  1. I retrieve a page, for example '/foo/bar/5/edit'.

  2. Django will call 'foo.views.editbar' with pk=5.

  3. This view renders an template 'foo/bar_form.html'

Situation 2:

  1. If I retrieve '/foo/bar/new'

  2. Django will call 'foo.views.newbar'

  3. This view renders the same template as above ('foo/bar_form.html')

How can I check in this template 'foo/bar_form.html', from which view it has been rendered?
The result should be one of

  • 'foo.views.editbar'

  • 'foo.views.newbar'


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.