[英]Django: Display website name in Template/Emails without using Sites framework
I want to render my website name in django templates. 我想在Django模板中呈现我的网站名称。 Django's own docs on Sites state : Django在Sites上的文档状态如下:
Use it if your single Django installation powers more than one site and you need to differentiate between those sites in some way. 如果单个Django安装为多个站点提供支持,并且您需要以某种方式区分这些站点,请使用它。
My django app doesn't. 我的Django应用没有。 I know I can still use it, but it seems like an overkill. 我知道我仍然可以使用它,但这似乎有点过头了。 I just want to pull a variable with my website's name (!= domain) in ANY template. 我只想在ANY模板中提取带有我的网站名称(!=域)的变量。 I don't want to pass it in views either because that doesn't seem DRY enough. 我也不想在视图中传递它,因为这似乎还不够干。
Writing a custom processor seemed like a simple-enough option, but for some reason these variables aren't available in the .txt emails django-registration sends (while other variables definitely are, so I guess it's not impossible). 编写自定义处理器似乎很简单,但是由于某些原因,这些变量在django-registration发送的.txt电子邮件中不可用(而其他变量肯定是,所以我想并非不可能)。
TIA TIA
Edit: was asked to include code that doesn't work: 编辑:被要求包含无效代码:
processors.py: processors.py:
def get_website_name(request):
website_name = 'SomeWebsite'
return {'mysite_name': website_name}
Included successfully in context_processors
in settings.py. 已成功包含在settings.py中的context_processors
中。 It works nicely in "regular" templates, but not in emails. 它可以在“常规”模板中很好地工作,但在电子邮件中则不能。
Here's how I'm sending the emails, inside a change_email_view
: 这是我在change_email_view
内发送电子邮件的change_email_view
:
msg_plain = render_to_string('email_change_email.txt', context)
msg_html = render_to_string('email_change_email.html', context)
send_mail(
'Email change request',
msg_plain,
'my@email',
[profile.pending_email],
html_message=msg_html,
)
A further problem is that django-regitration further abstracts some of those views away: so when a user registers, wants to reset a password, etc...I don't even have access to the views. 另一个问题是django-regitration进一步将其中的某些视图抽象了:因此,当用户注册,想重置密码等时,我什至无法访问这些视图。
Based on Django custom context_processors in render_to_string method you should pass the request to render_to_string
. 基于render_to_string方法中的Django自定义context_processors,您应该将请求传递给render_to_string
。
msg_plain = render_to_string('email_change_email.txt', context, request=request)
msg_html = render_to_string('email_change_email.html', context, request=request)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.