【发布时间】:2019-12-11 00:40:35
【问题描述】:
我想将图像添加到电子邮件的 html 模板中。在我的项目设置中,我有
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
和
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.media',
],
},
},
]
在我的网址里有
urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我的项目结构
我的电子邮件方法
message = render_to_string('recovery_password.html', {
"code": code,
"MEDIA_URL": settings.MEDIA_URL
})
mail_subject = 'Recovery password'
email = EmailMessage(mail_subject,
message,
to=[to_email],
)
email.content_subtype = "html"
email.send()
在 recovery_password.html 中我有图片上的链接
<a><img src="{{MEDIA_URL}}image1.png/></a>
但我在电子邮件中的图像不起作用。它不显示在模板中
【问题讨论】:
-
当你像这样模糊你的目录结构时,有点难以分辨。我只能看到您已将目录配置为“模板”并将其命名为“媒体”。但那里也有一个子目录。约定将您的模板存储为
// /template1..n.html 以避免在 2 个应用程序使用相同的模板名称时造成混淆。 -
@urbanespaceman 感谢您的评论。我有一个带有模板的单独文件夹和带有图像的单独文件夹。请查看我的 UPD 问题