Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FieldFile' object has no attribute 'rfind'

Im really new to Django, so Im still getting used to forms. I am trying to send an email in Django, using all the cleaned data from the form including a file that is uploaded, and I am getting the error "FieldFile' object has no attribute 'rfind'". when I try to attach a file to the email. Does that mean that the file has to be uploaded to a folder in my project first so the filepath has something to reference ?

Here is my form

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)

My View

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,
                                 ['[email protected]'])
            email.attach_file(applicant.resume)
            email.send(fail_silently=False)


        else:
            is_valid = False

    else:
        form = Application()
        Post = False
        is_valid = False
like image 964
TJB Avatar asked Mar 15 '16 19:03

TJB


1 Answers

attach_file() takes a path as argument, not FieldFile. This should be:

email.attach_file(applicant.resume.path)
like image 51
Antoine Pinsard Avatar answered Sep 30 '22 05:09

Antoine Pinsard