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.)
(这里的目标是,如果我重命名一个应用程序,那么我需要编辑的地方的数量最少。)
2 个解决方案
#1
17
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'
:
有一种方法可以获取当前请求的应用程序名称。首先,在项目的url中。考虑到你的应用叫“main”,py:
#urls.py
url(r'^', include('main.urls', app_name="main")),
Then, a context processsor:
然后,一个上下文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 }}
.
您可以在模板中像其他变量一样使用它:{{appname}。
#2
19
Since Django 1.5 there is a "resolver_match" attribute on the request object.
因为Django 1.5在请求对象上有一个“resolver_match”属性。
https://docs.djangoproject.com/en/dev/ref/request-response/
https://docs.djangoproject.com/en/dev/ref/request-response/
This contains the matched url configuration including "app_name", "namespace", etc.
它包含匹配的url配置,包括“app_name”、“namespace”等。
https://docs.djangoproject.com/en/1.6/ref/urlresolvers/#django.core.urlresolvers.ResolverMatch
https://docs.djangoproject.com/en/1.6/ref/urlresolvers/ django.core.urlresolvers.ResolverMatch
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.
唯一要注意的是,直到第一个中间件通过之后才填充它,因此在中间件的process_request函数中不可用。但是,它可以在中间件process_view、视图和上下文处理器中使用。而且,似乎resolver_match在错误处理程序中没有填充。
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
}
#1
17
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'
:
有一种方法可以获取当前请求的应用程序名称。首先,在项目的url中。考虑到你的应用叫“main”,py:
#urls.py
url(r'^', include('main.urls', app_name="main")),
Then, a context processsor:
然后,一个上下文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 }}
.
您可以在模板中像其他变量一样使用它:{{appname}。
#2
19
Since Django 1.5 there is a "resolver_match" attribute on the request object.
因为Django 1.5在请求对象上有一个“resolver_match”属性。
https://docs.djangoproject.com/en/dev/ref/request-response/
https://docs.djangoproject.com/en/dev/ref/request-response/
This contains the matched url configuration including "app_name", "namespace", etc.
它包含匹配的url配置,包括“app_name”、“namespace”等。
https://docs.djangoproject.com/en/1.6/ref/urlresolvers/#django.core.urlresolvers.ResolverMatch
https://docs.djangoproject.com/en/1.6/ref/urlresolvers/ django.core.urlresolvers.ResolverMatch
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.
唯一要注意的是,直到第一个中间件通过之后才填充它,因此在中间件的process_request函数中不可用。但是,它可以在中间件process_view、视图和上下文处理器中使用。而且,似乎resolver_match在错误处理程序中没有填充。
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
}