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

标签 django forms file email

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

相关文章:

python - 在for循环中按字典值分组

c# - 无法始终如一地将表格放在首位

javascript - 用本地镜像填充 svg 元素

java - zip 文件中的文件名太长 java

python - 将表单输入的行连接成一行

django - 如何在 Django 中命名模型外键字段?

python - 导入项目根以上的模块

php - HTML 表单 POST 到 PHP 页面

javascript - React 如何跳转到表单中的目标输入区域?

java - 在服务器端验证文件