【问题标题】:FieldFile' object has no attribute 'rfind'FieldFile' 对象没有属性 'rfind'
【发布时间】:2016-07-01 11:02:42
【问题描述】:

我对 Django 很陌生,所以我还是习惯了表单。我正在尝试在 Django 中发送一封电子邮件,使用表单中所有已清理的数据,包括上传的文件,我收到错误“FieldFile' object has no attribute '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

【问题讨论】:

    标签: django forms file email


    【解决方案1】:

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

    email.attach_file(applicant.resume.path)
    

    【讨论】:

    • 啊,我明白了。是的,它解决了这个问题,我很感激!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多