显示了我的示例项目。但提交时会出错。我已经在表单字段中添加了enctype。但没办法。请帮助:)
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 = 'anishmenon@me.com'
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))
我在提交表单时遇到错误
未提交任何文件。检查表单上的编码类型。
答案 0 :(得分:1)
您需要使用request.POST
和request.FILES
来实例化您的表单。
form = CarrerApplication(request.POST, request.FILES)
有关详细信息,请参阅binding uploaded files to a form上的文档。
答案 1 :(得分:1)
这样做会很酷
form = CarrerApplication((request.POST,request.FILES) or None)
if request.method == 'POST':
.......
答案 2 :(得分:0)
我也遇到了这个问题,我将FileInput更改为ClearableFileInput。
applicant_resume = forms.FileField(widget=forms.ClearableFileInput(attrs={'name':'applicant_resume'}),label='Resume')