[Django]-Django: How can I identify the calling view from a template?

19πŸ‘

βœ…

No, and it would be a bad idea. To directly refer to a view function name from the template introduces overly tight coupling between the view layer and the template layer.

A much better solution here is Django’s template inheritance system. Define a common parent template, with a block for the (small) area that needs to change in each view’s version. Then define each view’s template to extend from the parent and define that block appropriately.

πŸ‘€Carl Meyer

36πŸ‘

Since Django 1.5, the url_name is accessible using:

request.resolver_match.url_name

Before that, you can use a Middleware for that :

from django.core.urlresolvers import resolve

class ViewNameMiddleware(object):  
    def process_view(self, request, view_func, view_args, view_kwargs):
        url_name = resolve(request.path).url_name
        request.url_name = url_name

Then adding this in MIDDLEWARE_CLASSES, and in templates I have this:

{% if request.url_name == "url_name" %} ... {% endif %}

considering a RequestContext(request) is always passed to the render function. I prefer using url_name for urls, but one can use resolve().app_name and resolve().func.name, but this doesn’t work with decorators – the decorator function name is returned instead.

πŸ‘€Tisho

7πŸ‘

If your naming is consistent in your urls.py and views.py, which it should be, then this will return the view name:

{{ request.resolver_match.url_name }}

Be sure to apply some context to it when you call it in the template. For example, I use it here to remove the delete button from my detail view, but in my update view the delete button will still appear!

{% if request.resolver_match.url_name != 'employee_detail' %}
πŸ‘€Kermit

2πŸ‘

Since Django 1.5 you can access an instance of ResolverMatch through request.resolver_match.

The ResolverMatch gives you the resolved url name, namespace, etc.

πŸ‘€Josep Anguera

1πŸ‘

one simple solution is :

def view1(req):
   viewname = "view1"
   and pass this viewname to the template context   

def view2(req):
   viewname = "view2"
   and pass this viewname to the template context   

in template access the viewname as

{{viewname}} 

and also you can use this in comparisons.

1πŸ‘

This sounds like the perfect example of a generic view that you can set up.

See the following resources:

These links should help you simplify your views and your templates accordingly.

πŸ‘€Nick Presta

1πŸ‘

I’m working on this for a help-page system where I wanted each view to correspond to a help-page in my cms with a default page shown if no help page was defined for that view. I stumbled upon this blog where they use a template context processor and some python inspect magic to deduce the view name and populate the context with it.

πŸ‘€Jens Alm

1πŸ‘

If you’re using Class Based Views, you most likely have a view variable you can access.

You can use several methods from that to determine which view has been called or which template is being rendered.

e.g.

{% if view.template_name == 'foo.html' %}
# do something
{% else %}
# other thing
{% endif %}

Another option is to take out the piece of the template where you need something to change and make it into a snippet and then use {% include 'my_snippet.html' with button_type = 'bold' %} in your templates, sending arbitrary values to the snippet so it can determine what to show / how to style itself.

πŸ‘€getup8

1πŸ‘

In your template, you can access the current view instance like this:

{{ view }}

Define class_name method in your view

class ExampleView(FormView):
    ...
    def class_name(self):
        return self.__class__.__name__

You can get the class name of the current view in a template like this:

{{ view.class_name }}

{% if view.class_name == "ExampleView" %} ... {% endif %}
πŸ‘€Rufat

0πŸ‘

Most generic views β€” if not all β€” inherits the ContextMixin which adds a view context variable that points to the View instance.

-1πŸ‘

Why not trying setting up a session cookie, then read the cookie from your template.

on your views set cookies

def view1(request):
 ...
#set cookie
 request.session["param"]="view1"

def view2(request):
  request.session["param"]="view2"


then in your ONE template check something like..

{% ifequal request.session.param "view1" %}
   ... do stuff related to view1
{% endifequal %}

{% ifequal request.session.param "view2" %}
  ... do stuff related to "view2"
{% endifequal %}

Gath

πŸ‘€gath

Leave a comment