我有一些东西在设置。py,我想能够从模板访问,但我不知道如何做到这一点。我已经试过了
{{CONSTANT_NAME}}
但这似乎并不奏效。这可能吗?
我有一些东西在设置。py,我想能够从模板访问,但我不知道如何做到这一点。我已经试过了
{{CONSTANT_NAME}}
但这似乎并不奏效。这可能吗?
当前回答
上面来自bchhun的例子很好,只是你需要从settings.py显式地构建上下文字典。下面是一个未经测试的示例,说明如何从settings.py的所有大写属性(re: "^[A-Z0-9_]+$")自动构建上下文字典。
在settings.py的末尾:
_context = {}
local_context = locals()
for (k,v) in local_context.items():
if re.search('^[A-Z0-9_]+$',k):
_context[k] = str(v)
def settings_context(context):
return _context
TEMPLATE_CONTEXT_PROCESSORS = (
...
'myproject.settings.settings_context',
...
)
其他回答
将这段代码添加到名为context_processors.py的文件中:
from django.conf import settings as django_settings
def settings(request):
return {
'settings': django_settings,
}
然后,在你的设置文件中,包括一个路径,如'speed .core.base.context_processors。在TEMPLATES中的context_processors设置中的settings'(包含你的应用程序名称和路径)。
(例如,你可以看到settings/base.py和context_processors.py)。
然后可以在任何模板代码中使用特定的设置。例如:
{% if settings.SITE_ID == settings.SPEEDY_MATCH_SITE_ID %}
更新:上面的代码向模板公开了所有设置,包括敏感信息,如SECRET_KEY。黑客可能滥用此特性在模板中显示此类信息。如果你只想将特定的设置暴露给模板,请使用下面的代码:
def settings(request):
settings_in_templates = {}
for attr in ["SITE_ID", ...]: # Write here the settings you want to expose to the templates.
if (hasattr(django_settings, attr)):
settings_in_templates[attr] = getattr(django_settings, attr)
return {
'settings': settings_in_templates,
}
IanSR和bchhun都建议在设置中覆盖TEMPLATE_CONTEXT_PROCESSORS。请注意,这个设置有一个默认值,如果在不重新设置默认值的情况下重写它,可能会导致一些问题。在最近的Django版本中,默认值也发生了变化。
https://docs.djangoproject.com/en/1.3/ref/settings/#template-context-processors
默认的TEMPLATE_CONTEXT_PROCESSORS:
TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages")
一个更完整的实现。
/项目/ settings.py
APP_NAME = 'APP'
- app - templatetags settings_value . py
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag
def settings_value(name):
return getattr(settings, name, "")
/app/templates/index.html
<!DOCTYPE html>
{% load static %}
{% load settings_value %}
<head>
<title>{% settings_value "APP_NAME" %}</title>
...
如果你希望每个请求和模板都有一个值,那么使用上下文处理器更合适。
方法如下:
Make a context_processors.py file in your app directory. Let's say I want to have the ADMIN_PREFIX_VALUE value in every context: from django.conf import settings # import the settings file def admin_media(request): # return the value you want as a dictionnary. you may add multiple values in there. return {'ADMIN_MEDIA_URL': settings.ADMIN_MEDIA_PREFIX} add your context processor to your settings.py file: TEMPLATES = [{ # whatever comes before 'OPTIONS': { 'context_processors': [ # whatever comes before "your_app.context_processors.admin_media", ], } }] Use RequestContext in your view to add your context processors in your template. The render shortcut does this automatically: from django.shortcuts import render def my_view(request): return render(request, "index.html") and finally, in your template: ... <a href="{{ ADMIN_MEDIA_URL }}">path to admin media</a> ...
对于那些想要使用@Berislav的方法(自定义模板标签)和if标签的人:
/ app / templatetags / my_settings.py:
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag
def settings_value(name):
return getattr(settings, name, "")
模板文件:
<!-- Load your tags -->
{% load my_settings %}
{% settings_value 'ENABLE_FEATURE_A' as ENABLE_FEATURE_A %}
{% if ENABLE_FEATURE_A %}
<!-- Feature A stuffs -->
{% endif %}