我想使用芹菜发送带有照片的电子邮件,但我得到的"类型字节的对象不是JSON serializable"错误。我只是不知道该怎么做。据我所知,芹菜不支持字节。但我试图将其转换为字符串并得到其他错误。
views.py
from main_app.tasks import send_creator_submission_email_task
class BecomeCreatorView(TemplateView):
template_name = 'become_creator.html'
become_creator_form = BecomeCreatorForm
success_url = reverse_lazy('index')
def get(self, request, *args, **kwargs):
form = self.become_creator_form()
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.become_creator_form(request.POST, request.FILES)
if form.is_valid():
photo = form.cleaned_data['photo']
send_creator_submission_email_task.delay(self.kwargs.get('slug'), photo.file, photo.name)
messages.success(request, "Thank you!")
return redirect(self.success_url)
messages.error(request, "Error!")
return render(request, self.template_name, {'form': form})
tasks.py
@shared_task
def send_creator_submission_email_task(slug, image_file, image_filename):
return send_creator_submission_email(slug, image_file, image_filename)
email.py
def send_creator_submission_email(slug, image_file, image_filename):
user = get_object_or_404(User, slug=slug)
current_site = Site.objects.get_current().domain
email_context = {
'current_site': current_site,
'user_email': user.email,
'user_slug': user.slug,
'approve_url': f"http://{current_site}{reverse_lazy('approve_user', kwargs={'slug': user.slug})}",
"decline_url": f"http://{current_site}{reverse_lazy('decline_user', kwargs={'slug': user.slug})}"
}
html_message = render_to_string('email/admin_decision_email.html', email_context)
plain_message = strip_tags(html_message)
email = EmailMultiAlternatives(
subject="New creator submission",
body=plain_message,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[settings.EMAIL_ADMIN],
)
email.attach_alternative(html_message, "text/html")
email.attach(image_filename, image_file.read())
email.send()
如果你想重新创建这个案例,这些文件是给你的:forms.py
class BecomeCreatorForm(forms.Form):
photo = forms.ImageField(
required=True,
widget=forms.ClearableFileInput
)
admin_decision_email.html
{% autoescape off %}
<p>New user photo submission from: {{ user_email }}</p>
<p>User slug: {{ user_slug }}</p>
<p>Please approve or decline the attached photo.</p>
<p>Actevate: {{ approve_url }}</p>
<p>Decline: {{ decline_url }}</p>
{% endautoescape %}
所以,我自己处理了。我会把答案留给后人。
import base64
对视图中的图像进行编码,并将其传递给芹菜函数。
if form.is_valid():
photo_data = form.cleaned_data['photo'].read()
photo_name = form.cleaned_data['photo'].name
photo_data_base64 = base64.b64encode(photo_data).decode()
send_creator_submission_email_task.delay(self.kwargs.get('slug'), photo_data_base64, photo_name)
messages.success(request, "Thank you!")
return redirect(self.success_url)
在芹菜函数中解码,并附加到电子邮件中:
photo_data = base64.b64decode(attachment_data)
email.attach(attachment_name, photo_data)
这是它!