【问题标题】:Tips in Django Template for send an email with absolute pathDjango 模板中使用绝对路径发送电子邮件的提示
【发布时间】:2013-01-10 03:19:21
【问题描述】:
我对 Django 中的模板有疑问。我创建了一个通过 html 电子邮件发送的模板,该模板从站点中所有页面的相同 base.html 扩展而来(继承了样式、页眉和页脚)。
我的问题是:如果我在我的站点中,所有 URL 都是相对的,并且工作正常,但电子邮件没有看到任何内容,因为它在域之外。
我应该使用绝对路径复制电子邮件的页眉和页脚吗?我应该将应用程序中的所有路径从相对更改为绝对吗?渲染模板时是否有任何过滤器可以更改 URL?
我希望有人可以提供建议/提示。
问候,
克里斯蒂安
【问题讨论】:
标签:
django
django-templates
【解决方案1】:
示例:
views.py
from django.core.mail import send_mail
from django.template.loader import render_to_string
def notify_admin(request, topic):
message = render_to_string('emails/new_speakup_topic.txt', {
'topic': topic, 'ip_address': get_ip(request)})
subject = "Topic: {0}".format(topic.title)
send_mail(subject, message, from_email,
['to_email',])
在 templates 文件夹中创建 emails 文件夹,然后将此 txt 放入 emails 文件夹中
sample.txt
{{ topic.title }}
Category: {{ topic.category }}
Author: {{ topic.author.username }}
IP: {{ ip_address }}
{{ topic.description }}
https://site.com/speakup/topic/{{ topic.id }}/