Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

django

Short version:

Is there a simple, built-in way to identify the calling view in a Django template, without passing extra context variables?

Long (original) version:

One of my Django apps has several different views, each with its own named URL pattern, that all render the same template. There's a very small amount of template code that needs to change depending on the called view, too small to be worth the overhead of setting up separate templates for each view, so ideally I need to find a way to identify the calling view in the template.

I've tried setting up the views to pass in extra context variables (e.g. "view_name") to identify the calling view, and I've also tried using {% ifequal request.path "/some/path/" %} comparisons, but neither of these solutions seems particularly elegant. Is there a better way to identify the calling view from the template? Is there a way to access to the view's name, or the name of the URL pattern?


Update 1: Regarding the comment that this is simply a case of me misunderstanding MVC, I understand MVC, but Django's not really an MVC framework. I believe the way my app is set up is consistent with Django's take on MVC: the views describe which data is presented, and the templates describe how the data is presented. It just happens that I have a number of views that prepare different data, but that all use the same template because the data is presented the same way for all the views. I'm just looking for a simple way to identify the calling view from the template, if this exists.

Update 2: Thanks for all the answers. I think the question is being overthought -- as mentioned in my original question, I've already considered and tried all of the suggested solutions -- so I've distilled it down to a "short version" now at the top of the question. And right now it seems that if someone were to simply post "No", it'd be the most correct answer :)

Update 3: Carl Meyer posted "No" :) Thanks again, everyone.

like image 591
bryan Avatar asked Apr 27 '09 06:04

bryan


People also ask

Can I call a view from another view in Django?

To call a view from within another view with Python Django, we can call the view function directly. to call view1 in view2 . And then we can get the response returned and do what we want with it before we return the response. We can also return the response directly in view2`.

How do you pass variables from Django view to a template?

How do you pass a Python variable to a template? And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.

What does include does in Django template?

include tag loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.


1 Answers

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.

like image 102
Tisho Avatar answered Sep 28 '22 14:09

Tisho