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.)


There's a way to obtain an app name for a current request. First, in your project's, considering your app is called 'main':
url(r'^', include('main.urls', app_name="main")),

Then, a context processsor:

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:

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

Since Django 1.5 there is a "resolver_match" attribute on the request object.

This contains the matched url configuration including "app_name", "namespace", etc.

The only caveat is that it is not populated until after the first middleware passthrough, so is not available in process_request functions of middleware. However it is available in middleware process_view, views, and context processors. Also, seems like resolver_match is not populated in error handlers.

Example context processor to make available in all templates:

def resolver_context_processor(request):
    return {
        'app_name': request.resolver_match.app_name,
        'namespace': request.resolver_match.namespace,
        'url_name': request.resolver_match.url_name

Need Your Help

Why can't VS2008 place an exception helper popup onto a secondary monitor?

visual-studio-2008 multiple-monitors

I've recently acquired a second monitor and now run VS2008 SP1 maximized on my secondary (and bigger) monitor. This theoretically has the benefit of opening the application under development on the

How to remove data- elements from html

javascript html5

I have an href with data-toggle="dropdown", which I'm trying to remove.