Muh*_*bas 8 html email django static templates
我正在尝试使用 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()
Run Code Online (Sandbox Code Playgroud)
我的模板('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>
Run Code Online (Sandbox Code Playgroud)
该图像位于我的应用程序文件夹的 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/'
Run Code Online (Sandbox Code Playgroud)
请回答我缺少什么?
谢谢,
正如漫游所提到的,您缺少主机名+端口。
对于那些使用 Django 的 allauth 包进行身份验证的人(https://www.djangopackages.com/packages/p/django-allauth/),您可以简单地使用 {{site}},例如:
<img src="{{site}}{{ STATIC_URL }}images/footer.jpg" alt="" />
Run Code Online (Sandbox Code Playgroud)
或者更好的是,使用 %static 注释:
{% load static %}
...
<img src="{{site}}{% static 'images/footer.jpg' %}" alt="" />
Run Code Online (Sandbox Code Playgroud)
您需要指定完整的STATIC_URL
,例如http://localhost:8000/static/
。现在该消息指向/static/images/footer.jpg
的 URL 无效。
归档时间: |
|
查看次数: |
7989 次 |
最近记录: |