gpt4 book ai didi

django - 在 Django 中发送带附件的电子邮件

转载 作者:行者123 更新时间:2023-11-28 19:37:41 24 4
gpt4 key购买 nike

我正在尝试发送带有 Django 中附加的一些图像的电子邮件。使用的代码是这个片段:http://www.djangosnippets.org/snippets/1063/ .我不知道,为什么附件部分会返回一个核心错误。

代码。 表单.py

from django import forms
from common import slugify_unique
from django.conf import settings
from django.core.cache import cache
from django.contrib.admin import widgets
from django.shortcuts import get_object_or_404

class WorkForm(forms.Form):
name = forms.CharField(label='Name and surname', max_length=64, required = True )
nick = forms.CharField(label='nickname', max_length=40, required = True )
email = forms.EmailField(label='e-mail', required = True )
image1 = forms.Field(label='sample photo', widget = forms.FileInput, required = True )
image2 = forms.Field(label='sample photo', widget = forms.FileInput, required = True )
image3 = forms.Field(label='sample photo', widget = forms.FileInput, required = True )
text = forms.CharField(label='Few words about you', widget=forms.Textarea, required = False )

View .py

from forms import WorkForm
from django.core.mail import send_mail, EmailMessage


def work(request):
template = 'other/work.html'

if request.method == 'POST':
form = WorkForm(request.POST, request.FILES)
if form.is_valid():
name = form.cleaned_data['name']
nick = form.cleaned_data['nick']
email = form.cleaned_data['email']
subject = 'Work'
text = form.cleaned_data['text']
image1 = request.FILES['image1']
image2 = request.FILES['image2']
image3 = request.FILES['image3']
try:
mail = EmailMessage(subject, text, ['EMAIL_ADDRESS'], [email])
mail.attach(image1.name, attach.read(), attach.content_type)
mail.attach(image2.name, attach.read(), attach.content_type)
mail.attach(image3.name, attach.read(), attach.content_type)
mail.send()
template = 'other/mail_sent.html'
except:
return "Attachment error"
return render_to_response(template, {'form':form},
context_instance=RequestContext(request))
else:
form = WorkForm()
return render_to_response(template, {'form':form},
context_instance=RequestContext(request))

这是错误站点图片: http://img201.imageshack.us/img201/6027/coreerror.png我做错了什么?

最佳答案

您发布的错误回溯似乎与实际代码没有任何关系 - 它似乎是中间件的某种问题(大概是在呈现 500 错误页面时)。

但是,您的错误可能是由于您在调用 mail.attach 时使用了 undefined variable 名 attach 引起的。您没有 attach 变量 - 您已将发布的文件命名为 image1 等,因此您应该使用这些名称。

mail.attach(image1.name, image1.read(), image1.content_type)
mail.attach(image2.name, image2.read(), image2.content_type)
mail.attach(image3.name, image3.read(), image3.content_type)

关于django - 在 Django 中发送带附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2457906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com