[英]Django Sending HTML Email with Images
我正在尝试使用 django 1.3 中的图像发送 HTML 电子邮件,但我的图像未加载。 以下是我的view.py
email_data = {
# Summary data
'user_name':user_name,
'points':user.numOfPoints,
}
email_data = RequestContext(request, email_data)
t = get_template('user_email.html')
content = t.render(email_data)
msg = EmailMessage(subject='User Email', content, 'admin@company.com', ['user@gmail.com'])
msg.content_subtype = "html"
msg.send()
我的模板('user_email.html')如下所示
<html>
<body>
Hello {{user_name}},
Your point score is: {{points}}
Thank you for using our app.
<img src="{{ STATIC_URL }}images/footer.jpg" alt="" />
<body>
</html>
该图像位于我的应用程序文件夹的 static/image 文件夹中。 但是当收到电子邮件时,它没有图像。
在我的 settings.py 中,我有以下内容
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
)
STATIC_URL = '/static/'
请回答我缺少什么?
谢谢,
正如漫游所提到的,您缺少主机名+端口。
对于那些使用 Django 的 allauth 包进行身份验证的人( https://www.djangopackages.com/packages/p/django-allauth/ ),您可以简单地使用 {{site}},例如:
<img src="{{site}}{{ STATIC_URL }}images/footer.jpg" alt="" />
或者更好的是,使用 %static 注释:
{% load static %}
...
<img src="{{site}}{% static 'images/footer.jpg' %}" alt="" />
您需要指定完整的STATIC_URL
,例如http://localhost:8000/static/
。 现在消息指向/static/images/footer.jpg
这不是一个有效的 URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.