I am trying to send a 2 pdf files as attachment,1 generated by xhtml2pdf and another which is already present in the app directory of my django project, based on a condition. I am able to send the generated file and 1 file but getting error " File does not exist" for the other condition, even when both the files are in the same location. I am a newbie here, what am i doing wrong? where and how do i need to store the files to be attached ?
my views code for email-
patienthist = get_object_or_404(PatientHist, pk=email_id) # stores values (Y,N)
pdf = Render.render('eradicate/dietplan.html', context)
msg = EmailMessage("Your Diet Plan", "Attached is your diet plan!", to=[email_id])
msg.content_subtype = "html"
msg.attach('my_pdf.pdf', pdf, 'application/pdf') # attaching generated pdf
if patienthist.foodhabit == 'N':
msg.attach_file('eradicate/LCHF Eradicate Diabetes.pdf') # doest not get attached
else:
msg.attach_file('eradicate/LFV Eradicate Diabetes.pdf') # gets attached to email
msg.send()
error i am getting-
FileNotFoundError at /eradicate/test@gmail.com/dietplan/
[Errno 2] No such file or directory: 'eradicate\\LCHF Eradicate Diabetes.pdf'
Exception Location: C:\Users\user\AppData\Local\Programs\Python\Python37\lib\pathlib.py in _opener, line 1058
note: its adding an extra backslash after the app name eradicate, why does this happen?
my 2 files are stored in the django app directory, same level as that of views, models, admins etc.