I am trying to create a vacancy form where applicants can submit their CV. When I submit the form for some reason the file field gets cleared and tell me to attach one. I DO NOT want to include a database in this method of approach as I feel like it may consume space in the database. Is it possible to do that?
views.py
def contact_us(request):
if request.method == "POST":
form = ContactUsEmploymentForm(request.POST, request.FILES)
if form.is_valid():
first_name = form.cleaned_data.get('first_name')
last_name = form.cleaned_data.get('last_name')
designation = form.cleaned_data.get('designation')
expected_salary = form.cleaned_data.get('expected_salary')
cv = form.cleaned_data.get('cv')
email = form.cleaned_data.get('email')
content = form.cleaned_data.get('content')
content = f""" WARNING: THIS IS AN EMAIL SENT BY A USER \n
First name: {first_name}
Last name: {last_name}
User's Email: {email}\n
Applying for: {designation}
Expected salary: {expected_salary}
Body:
{content}
"""
send_mail(
"Candidate applying for a vacancy",
content,
'my_email',
['my_email'],
fail_silently=False,
)
return render(request, "homepage/email_confirmation.html")
else:
form = ContactUsEmploymentForm()
return render(request, "homepage/contact_us.html", {"form":form})
forms.py
class ContactUsEmploymentForm(forms.Form):
first_name = forms.CharField(max_length=80)
last_name = forms.CharField(max_length=80)
email = forms.EmailField()
designation = forms.ModelChoiceField(queryset=Vacancy.objects.filter().all())
expected_salary = forms.CharField(max_length=80)
cv = forms.FileField()
content = forms.CharField(max_length=800, widget=forms.Textarea)
and the HTML file
<div class="contact_us_container p-2">
<div class="contact_us_wrapper mt-5 mb-2">
<h2 class="font_1 big_font mt-5">Talk to us today!</h2>
<div class="container">
<form method="POST">
{% csrf_token %}
{{form | crispy}}
<button class="btn btn-outline-dark ">Send</button>
</form>
</div>
</div>
</div>
Any help would be greatly appreciated. Thanks!
The Django docs says the following:
Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data".
example: form method="post" enctype="multipart/form-data"
see:
https://docs.djangoproject.com/en/3.0/topics/http/file-uploads/