3
votes

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?

Thanks

EDIT

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'
3
what are you trying for?Leandro
Can you explain more your question? and if it's possible put code pleaseVictor Castillo Torres

3 Answers

16
votes

Type just in view

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

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

inspect.stack()[1][3]
-3
votes

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.