[英]Django Sending HTML Email with Images
I am trying to send an HTML email with images from django 1.3 but my Images are not loading.我正在尝试使用 django 1.3 中的图像发送 HTML 电子邮件,但我的图像未加载。 Following is my view.py
以下是我的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()
My template('user_email.html') looks like following我的模板('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>
The image is place inside the static/image folder of my app folder.该图像位于我的应用程序文件夹的 static/image 文件夹中。 But when email is received it is without the image.
但是当收到电子邮件时,它没有图像。
In my settings.py, I have following在我的 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/'
Kindly respond what I am missing?请回答我缺少什么?
Thanks,谢谢,
As mentioned by roam you are missing the hostname+port.正如漫游所提到的,您缺少主机名+端口。
For those who are using Django's allauth package for authentication ( https://www.djangopackages.com/packages/p/django-allauth/ ), you can simply use {{site}}, eg:对于那些使用 Django 的 allauth 包进行身份验证的人( https://www.djangopackages.com/packages/p/django-allauth/ ),您可以简单地使用 {{site}},例如:
<img src="{{site}}{{ STATIC_URL }}images/footer.jpg" alt="" />
Or better yet, using the %static annotation:或者更好的是,使用 %static 注释:
{% load static %}
...
<img src="{{site}}{% static 'images/footer.jpg' %}" alt="" />
You'll need to specify the full STATIC_URL
, eg http://localhost:8000/static/
.您需要指定完整的
STATIC_URL
,例如http://localhost:8000/static/
。 Right now the message points to /static/images/footer.jpg
which is not a valid URL.现在消息指向
/static/images/footer.jpg
这不是一个有效的 URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.