我正在尝试使用联系表格附加文件。对于表单和视图,我的代码如下所示:
if request.method == 'POST':
form = UploadCVForm(request.POST, request.FILES)
if form.is_valid(): # All validation rules pass
subject = "CV Sent from BiztechAfrica"
sender = form.cleaned_data['email']
message = "Some message goes in here"
name = form.cleaned_data['name']
recipients = ['herman@xxx.co.za']
cv_file = request.FILES['cv_file']
mail = EmailMessage(subject, message, sender, recipients)
mail.send()
return HttpResponse('Thanks') # Redirect after POST
else:
form = UploadCVForm()
这是我的forms.py:
class UploadCVForm(forms.Form):
subject = "CV Sent from BiztechAfrica"
name = forms.CharField(max_length=128)
email = forms.EmailField()
cv_file = forms.Field(label='CV', widget = forms.FileInput, required = True )
电子邮件工作正常,但我无法附加文件,因为它一直给我一个表单错误,即尽管我选择了要上传的文件,但文件上传输入字段不能为空。
有任何想法吗?我仍然是 Django 和 Python 的新手......