gpt4 book ai didi

django - FieldFile' 对象没有属性 'rfind'

转载 作者:行者123 更新时间:2023-12-04 15:54:14 25 4
gpt4 key购买 nike

我对 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

最佳答案

attach_file()将路径作为参数,而不是 FieldFile .这应该是:

email.attach_file(applicant.resume.path)

关于django - FieldFile' 对象没有属性 'rfind',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36020893/

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