如何添加上下文到Django中的activation_email.txt [英] How to add context to the activation_email.txt in Django
问题描述
我想通过添加一些已经可用于所有其他页面的变量来自定义 registration / activation_email.txt
模板(例如: my_var
)。它们可用于其他模板,因为我将它们添加到 context_processors.py
和 TEMPLATE_CONTEXT_PROCESSORS
中 setting.py
。
所以我可以在所有模板上都有 {{my_var}}
。
I want to customize registration/activation_email.txt
template by adding some variables that are already available to all other pages (for example: my_var
). They are available to the other templates because I added them to context_processors.py
and in TEMPLATE_CONTEXT_PROCESSORS
at setting.py
.
So I can have {{ my_var }}
on all templates.
但是我不能从 activation_email.txt
模板内到达 {{my_var}}
根据文档此处
But I can't reach {{ my_var }}
from inside activation_email.txt
template. According to a doc here
此模板有3个上下文: activation_key
, expiration_days
和站点
。如何在这里添加更多的上下文?
This template has 3 contexts: activation_key
, expiration_days
and site
. How do I add more context here?
推荐答案
该应用程序不会简单地暴露钩子来添加更多上下文或更改使用的上下文类型构建电子邮件 - 最好的方法是将其RegistrationProfile类(实际构建并发送电子邮件)和至少基于RegistrationView类的视图进行子类化,然后确保您的urls.txt正在调用视图子类。如下:
That app does not trivially expose hooks to add more context or change the context type used to build the email - your best bet is to subclass its RegistrationProfile class (which actually builds and sends the email message) and at least the RegistrationView class-based view, then make sure your urls.txt is calling your view subclass instead. Something like:
from registration.models import RegistrationProfile
from django.template import RequestContext
class VerboseRegistrationProfile(RegistrationProfile):
def send_activation_email(self, site):
ctx_dict = {'activation_key': self.activation_key,
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
'site': site,
subject = render_to_string('registration/activation_email_subject.txt',
ctx_dict, context=RequestContext())
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
message = render_to_string('registration/activation_email.txt',
ctx_dict, context=RequestContext())
self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
然后您的视图:
from myreg.models import VerboseRegistrationProfile
from registration.backends.default import RegistrationView
class VerboseRegistrationView(RegistrationView):
def register(self, request, **cleaned_data):
username, email, password = cleaned_data['username'], cleaned_data['email'], cleaned_data['password1']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = VerboseRegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
为了避免将来的维护问题,如果您进一步专注于您的RegistrationProfile子类,您可能还需要覆盖ActivationView,但并不是绝对必要的。
To avoid future maintenance problems in case you ever further specialize your RegistrationProfile subclass you'd probably also want to override ActivationView as well, but it's not strictly necessary.
这篇关于如何添加上下文到Django中的activation_email.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!