'Django Python: Get the uploaded file name and add it to the body of the email message
I'm using Django for a simple contact form with a file upload.
I'm not attaching the file to the email; I'm simply uploading it to the server via forms.py. But I need to add the file name in the body of the contact form email so I know which file has been uploaded by which email user.
How do I get the uploaded file name and add it to the body of the email?
Does file = fs.save(request_file.name, request_file) output the file name?
If not, how do I get the uploaded file name? And add it to the email?
I am also aware that Django adds a random string of characters to the uploaded file name if it sees an existing file with the same name n the upload directory, so in that case, I need that file name included.
views.py
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from django.shortcuts import render, get_object_or_404, redirect
from django.core.files.storage import FileSystemStorage
from contactform.forms import ContactForm
from contact.settings import EMAIL_HOST_USER, EMAIL_PORT, EMAIL_HOST_PASSWORD, EMAIL_HOST
def thanks(request):
return render(request, 'thanks.html', {})
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST,request.FILES)
request_file = request.FILES['document'] if 'document' in request.FILES else None
if request_file:
fs = FileSystemStorage()
file = fs.save(request_file.name, request_file)
fileurl = fs.url(file)
if form.is_valid():
form_data = form.cleaned_data
msg = MIMEMultipart()
msg['From'] = EMAIL_HOST_USER
msg['To'] = EMAIL_HOST_USER
msg['Subject'] = f'Site Email'
message = f'Name: {form_data["name"]}\n' \
f'Email address: {form_data["email_address"]}\n\n' \
f'{form_data["message"]}' // How do I insert the file name in the message body?
msg.attach(MIMEText(message))
with smtplib.SMTP(EMAIL_HOST, EMAIL_PORT) as server:
server.ehlo()
server.starttls()
server.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
server.sendmail(EMAIL_HOST_USER, EMAIL_HOST_USER, msg.as_string())
return redirect('contactform:thanks')
else:
form = ContactForm()
return render(request, 'contact.html', { "form": form })
Solution 1:[1]
No need to use another msg field; this works. All that is needed is file, since file = fs.save(request_file.name, request_file) gets the filename.
This also adds the correct file name in the message if Django has added random characters to prevent same name file overwrites:
message = f'Name: {form_data["name"]}\n' \
f'Email address: {form_data["email_address"]}\n\n' \
f'File Uploaded: {file}\n\n' \
f'{form_data["message"]}'
Solution 2:[2]
You can use request.FILES.get('file_name').name like this:
msg['Message'] = f'Hi, You uploaded {request.FILES.get('filename').name}. Thanks!'
Don't forget to add .get before file name
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BlueDogRanch |
| Solution 2 | enes islam |
