我的网站运行多个域。用户可以在这些域中的每一个上注册。因此,我需要制作 django-registration-redux:
-
使用正确的电子邮件地址发送注册/密码重置电子邮件
-
使用正确的电子邮件密码发送注册/密码重置电子邮件
-
在注册/密码重置电子邮件中使用正确的域
我一直在深入研究 django-registration-redux 的源代码,并相信我需要更新注册/模型中的send_email方法.py(https://github.com/macropin/django-registration/blob/master/registration/models.py)使用我所需的更改。
我假设添加此客户化的最佳方法如下:
- 运行'pip uninstall django-registration-redux==2.2'
-
运行"点冻结>要求.txt">
-
从源代码中,将"注册"文件夹拉入我的项目
-
进入我的项目/注册/模型.py并手动更新send_email方法,使其包含我的更改。
有没有更简单或更正确的方法可以在不进行上述更改的情况下将我的自定义逻辑构建到 def send_email 中?
谢谢!
您可以对模型进行子类化,然后覆盖调用send_email()
的函数,并使用自定义函数对其进行修补。
from unittest import mock
from django_registration import models
def custom_send_email():
print('sending custom email')
class CustomEmailRegistration(models.Registration):
def function_calling_send_email(self):
with mock.patch('models.send_email', side_effect=custom_send_email):
super().function_calling_send_email()
解决方案是添加一个上下文处理器,该处理器根据当前网站获取公司名称和网站,可以通过请求访问,然后在 django 注册电子邮件模板中引用上下文变量:)
以下是添加从请求中获取当前主机名的上下文处理器的方式:
将文件my_ctx_proc.py
添加到应用程序myapp
:
def get_info_email(request):
return {
'info_email': "info@" + request.META['HTTP_HOST'],
}
在您的settings.py
中,在末尾添加TEMPLATES
OPTIONS
context_processors
:
'myapp.my_ctx_proc.get_info_email',
现在,您可以在电子邮件模板中插入{{ info_email }}
,例如。templates/django_registration/activation_body_email.txt