该应用程序不会轻易暴露钩子以添加更多上下文或更改用于构建电子邮件的上下文类型 - 您最好的办法是继承其 RegistrationProfile 类(实际构建和发送电子邮件)和至少基于 RegistrationView 类的视图,然后确保您的 urls.txt 正在调用您的视图子类。就像是:
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,但这并不是绝对必要的。