Python {{domain}}在本地pc上成为example.com
我正在学习用django发送邮件,但我进入了这个问题,它返回Python {{domain}}在本地pc上成为example.com,python,django,Python,Django,我正在学习用django发送邮件,但我进入了这个问题,它返回http://example.com/而不是http://127.0.0.1:8000/ 发送邮件并生成链接的代码如下(我在两行上添加了注释,我怀疑这是原因): 我将如何更正这一点,因为函数应该使用该域。理想情况下,我可以在部署应用程序时在settings.py中定义它 先谢谢你 编辑!添加了电子邮件模板我认为您已经配置了django框架。因此,不要使用request.get\u host() 或者您可以将您的站点域从django管理面
http://example.com/
而不是http://127.0.0.1:8000/
发送邮件并生成链接的代码如下(我在两行上添加了注释,我怀疑这是原因):
我将如何更正这一点,因为函数应该使用该域。理想情况下,我可以在部署应用程序时在settings.py
中定义它
先谢谢你
编辑!添加了电子邮件模板我认为您已经配置了django框架。因此,不要使用
request.get\u host()
或者您可以将您的站点域从django管理面板更改为
127.0.0.1
从example.com
和get\u current\u站点(请求)。域将工作开始!我不知道。非常感谢你!
views.py
# Send activation E-mail
current_site = get_current_site(self.request) # Either this line
mail_subject = 'Activate your Penge account.'
message = render_to_string('emails/activation-mail.html', {
'user': user,
'domain': current_site.domain, # or maybe this line
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': default_token_generator.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address')
activation-mail.html
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'accounts:activate' uidb64=uid token=token %}
If you think, it's not you, then please ignore this email.
{% endautoescape %}
'domain': request.get_host(),