【问题标题】:Django - context_processorsDjango - 上下文处理器
【发布时间】: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


【解决方案1】:

罪魁祸首是PasswordResetForm 类的send_mail 方法。这里,render_to_string 用于构建电子邮件正文:

class PasswordResetForm(forms.Form):
    def send_mail(self, ...):
        # ...
        body = loader.render_to_string(email_template_name, context)
        # ...

如果您希望它通过您的上下文处理器,您需要使用PasswordResetForm 的自定义子类,您可以在其中覆盖send_mail 方法并为render_to_string 提供一个额外的关键字参数context_instance,它必须是RequestContext 实例:

        body = loader.render_to_string(
            email_template_name, context,
            context_instance=RequestContext(request)
        ) 
        # you might have to pass the request from the view 
        # to form.save() where send_mail is called

这适用于所有模板渲染。只有 RequestContext 实例通过上下文处理器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-15
    • 2010-11-04
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2015-11-01
    相关资源
    最近更新 更多