我真的很喜欢Django,所以我还是习惯了表格。我试图在Django中发送一封电子邮件,使用表单中的所有已清理数据,包括上传的文件,我收到错误“FieldFile'对象没有属性'rfind'”。当我尝试将文件附加到电子邮件时。这是否意味着必须首先将文件上传到我的项目中的文件夹,以便文件路径可以引用?
这是我的表格
class Application(forms.Form):
first_name = forms.CharField(label="First Name", max_length=50)
last_name = forms.CharField(label="Last Name", max_length=50)
email = forms.EmailField(label="Email", max_length=80)
phone = forms.CharField(label="Phone Number", max_length=30)
resume = forms.FileField(label="Resume", max_length=1000)
message = forms.CharField(label="Message", max_length=800, widget=forms.Textarea)
我的观点
if request.method == "POST":
form = Application(request.POST, request.FILES)
Post = True
if form.is_valid():
cleaned_data = form.cleaned_data
is_valid = True
applicant = Applicant()
applicant.first_name = cleaned_data['first_name']
applicant.last_name = cleaned_data['last_name']
applicant.email = cleaned_data['email']
applicant.phone = cleaned_data['phone']
applicant.resume = request.FILES['resume']
applicant.message = cleaned_data['message']
applicant.job = career.name
date = datetime.datetime.now()
applicant.save()
email_context = {'interested': applicant}
html_content = render_to_string("email/contact/application-html.html", email_context)
email = EmailMessage('Some is interested in a demo with Atlas', html_content, settings.DEFAULT_FROM_EMAIL,
['timbaney1989@gmail.com'])
email.attach_file(applicant.resume)
email.send(fail_silently=False)
else:
is_valid = False
else:
form = Application()
Post = False
is_valid = False
答案 0 :(得分:5)
attach_file()
将路径作为参数,而不是FieldFile
。这应该是:
email.attach_file(applicant.resume.path)