【发布时间】:2016-07-01 13:28:03
【问题描述】:
我正在尝试使用 context_processors 将 settings.py 中的一些配置转换为我的模板可用。
我用这样的方式创建了一个文件:
from django.conf import settings
def my_custom_var (request):
return {'MY_CUSTOM_VAR': settings.`MY_CUSTOM_PROP`}
这是我在 settings.py 上的模板配置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.i18n',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'my_app.py_file.my_custom_var',
],
},
},]
当我尝试在我的 html 模板上使用 {{ MY_CUSTOM_VAR }} 时,一切正常。但是当我尝试在电子邮件密码重置模板 (django/contrib/admin/templates/registration/password_reset_email.html) 上使用它时,MY_CUSTOM_VAR 的值为 null。
这是我的密码_reset_email.html:
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
MY_CUSTOM_VAR: {{ MY_CUSTOM_VAR }}
{% endblock %}
{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}
有谁知道怎么回事?还有其他方法吗?
谢谢!
【问题讨论】:
-
密码重置电子邮件不使用上下文处理器。您如何在 URLs.py 中包含视图?
-
我正在使用 Django Rest Auth。在我的 urls.py 中,我有这个条目:“url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))”,并且电子邮件是通过“rest-auth”中的 POST 发送的/重设密码/”。但我使用“自定义模板标签和过滤器”得到它。 docs.djangoproject.com/en/1.9/howto/custom-template-tags。谢谢。
-
我将建议自定义密码重置视图的 url 条目,以包含额外的上下文。但是,我认为自定义模板标签是一个很好的解决方案。
标签: django django-templates django-registration django-email