[英]Django Rest Framework Send Image in Email
Hello I have faced an Issue regarding sending email In django rest API.您好,我遇到了关于在 django rest ZDB974238714CA8DE634A7CE1D083A 中发送 email 的问题The Idea is user sends email and Uploads image from serializer part and I want to send same image to users Email.想法是用户发送 email 并从序列化程序部分上传图像,我想将相同的图像发送给用户 Email。 But I am not getting image in email.但我没有在 email 中获得图像。
here is the code I have been working.这是我一直在工作的代码。
models.py
class Mail(BaseModel):
full_name = models.CharField(max_length=100)
image = models.ImageField(upload_to=mail_image_to)
email = models.EmailField()
below is my serializer下面是我的序列化器
class MailSerializer(serializers.ModelSerializer):
class Meta:
model = Mail
fields = '__all__'
class AddMailSerializer(MailSerializer):
class Meta(MailSerializer.Meta):
fields = (
'full_name',
'image',
'email',
)
views.py视图.py
class AddMailView(generics.CreateAPIView):
"""
Use this endpoint to add mail
"""
serializer_class = serializers.AddMailSerializer
def perform_create(self, serializer):
return AddMailUseCase(serializer=serializer).execute()
I usually write my rest of code in usecase.py我通常在 usecase.py 中编写我的 rest 代码
class AddMailUseCase:
def __init__(self, serializer):
self.serializer = serializer
self.data = serializer.validated_data
def execute(self):
self._factory()
def _factory(self):
self._mail = Mail(**self.data)
self._mail.save()
SendEmail(
context={
"fullname": self.data['full_name'],
'image': self.data['image']
}
).send(to=[self.data['email']])
I am using django-templated-mail 1.1.1
to send email here is my rest of code.我正在使用django-templated-mail 1.1.1
发送 email 这是我的 rest 代码。
from templated_mail.mail import BaseEmailMessage
class SendEmail(BaseEmailMessage):
template_name = 'email.html'
and finally my email.html
最后是我的email.html
{% load i18n %}
{% block subject %}
{% blocktrans %}Email Successfully Sent {% endblocktrans %}
{% endblock subject %}
{% block text_body %}
{% blocktrans %}You're receiving this email because you recently registered to our website.{% endblocktrans %}
{% trans "Your submitted details are as follow" %}
{% endblock text_body %}
{% block html_body %}
{% blocktrans %}
<p>hello {{ fullname }}</p>{% endblocktrans %}
{% blocktrans %}
<img src="{{ image }}" alt="email-image">
<p>{% trans "Thanks for using our site!" %}</p>
{% endblock html_body %}
any help regarding this issue?关于这个问题的任何帮助? the email I got is as below image is not loading我得到的 email 如下图未加载
instead of using而不是使用
<img src="{{ image }}" alt="email-image">
use利用
<img src="{{ image.url }}" alt="email-image">
and in your URL.py add并在您的 URL.py 添加
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.