以下は私のサンプルプロジェクトを示しています。しかし、送信中にエラーが発生します。フォーム フィールドに 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>
フォーム.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
ビュー.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))
フォームの送信中にエラーが発生しました
ファイルは提出されませんでした。フォームのエンコード タイプを確認します。