below shows my example project. but it raise an error while submitting. i've added enctype to form field . but no way. please help :)
html
<form class="forms2" action="." method="post" enctype="multipart/form-data">{%csrf_token%}
<fieldset>
<ol>
{% for field in forms %}
<li class="form-row">
<b>{{ field.label_tag }}</b> {{ field }} </li>
<i>{{ field.errors }}</i>
{%endfor%}
<li class="button-row">
<input type="submit" value="Submit" name="submit" class="btn-submit" />
</li>
</ol>
</fieldset>
</form>
forms.py
class CarrerApplication(forms.Form):
applicant_name = forms.CharField(widget=forms.TextInput(attrs={'class':'text-input required'}),label='Name')
applicant_address = forms.CharField(widget=forms.TextInput(attrs={'class':'text-input required'}),label='Address')
applicant_email = forms.EmailField(widget=forms.TextInput(attrs={'class':'text-input required email'}),label='Email')
applicant_mobile = forms.CharField(widget=forms.TextInput(attrs={'class':'text-input'}),label='Mobile')
applicant_resume = forms.FileField(widget=forms.FileInput(attrs={'name':'applicant_resume'}),label='Resume')
applicant_message = forms.CharField(widget=forms.Textarea(attrs={'class':'text-area'}),label='Message')
def clean_applicant_resume(self):
applicant_resume = self.cleaned_data['applicant_resume']
exts = ['doc', 'docx', 'pdf', 'jpg', 'png', 'xls', 'xlsx', '.xlsm', '.xlsb']
max_email_attach_size = 10485760 #10MB written in bytes
if not applicant_resume in (None, ''):
try:
if applicant_resume.size > max_email_attach_size:
raise ValidationError("The file is too big")
file_extension = applicant_resume.name.split('.')[1]
if file_extension not in exts:
raise ValidationError("Invalid File Type")
except AttributeError:
raise ValidationError(self.error_messages['invalid'])
return applicant_resume
views.py
def careers(request):
if request.method == 'POST':
form = CarrerApplication(request.POST,request.FILES)
if form.is_valid():
applicant_name= request.POST.get('applicant_name')
applicant_address=request.POST.get('applicant_address')
applicant_mobile=request.POST.get('applicant_mobile')
applicant_email=request.POST.get('applicant_email')
applicant_resume=request.FILES.get('applicant_resume')
applicant_message = request.POST.get('applicant_message')
subject = 'Resume from Blaah blaah'
email = '[email protected]'
message_template = loader.get_template('resume_mail/resume_send.txt')
message_context = Context({ 'name': applicant_name,'message':applicant_message })
message = message_template.render(message_context)
mail = EmailMessage(subject, message, applicant_email, [email])
mail.attach(applicant_resume.name, applicant_resume.read(), applicant_resume.content_type)
mail.send()
return HttpResponse('Thanks')
else:
form = CarrerApplication()
return render_to_response('resume_mail/resume_apply.html',
{'forms' : form},
context_instance=RequestContext(request))
i'm getting an error while submitting form
No file was submitted. Check the encoding type on the form.
You need to instantiate your form with request.POST
and request.FILES
.
form = CarrerApplication(request.POST, request.FILES)
For more information see the docs on binding uploaded files to a form.
It will be cool to do so
form = CarrerApplication((request.POST,request.FILES) or None)
if request.method == 'POST':
.......
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With