How to get the name of current app within a template?

What's the simplest way to access the name of the current app within template code?

Alternatively, what's the simplest way to define a template variable to hold the name of the current app?

(The goal here is to minimize the number of places I need to edit if I rename an app.)

Answers


There's a way to obtain an app name for a current request. First, in your project's urls.py, considering your app is called 'main':

#urls.py
url(r'^', include('main.urls', app_name="main")),

Then, a context processsor:

#main/contexts.py
from django.core.urlresolvers import resolve
def appname(request):
    return {'appname': resolve(request.path).app_name}

Don't forget to enable it in your settings:

#settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"main.contexts.appname",)

You can use it in your template like any other variable: {{ appname }}.


Need Your Help

Do too many plug-ins slow down Eclipse?

java eclipse ide eclipse-plugin performance

I have recently installed a new project into my Eclipse and also needed to install some plug-ins to manage it. I also installed other plug-ins which I do not need to manage the project but thought it

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.