我有一個文件上傳表單,我無法驗證成功。 表單加載正常,我可以上傳文件,但'提交'後,我無法在視圖中通過Fileform.is_valid()。我主要從Django 1.9 File Uploads文檔複製這個視圖和模型和表單模型,但是我必須缺少一些東西。文件上傳表單無效
的view.py是,
def file_sharing_form(request):
if request.method == "POST":
file = FileForm(request.POST, request.FILES)
if file.is_valid():
fform = file.save(commit=False)
fform.author = request.user
fform.pub_date = timezone.now()
fform.submitted_date = timezone.now()
fform.approved = False
fform.save()
# email admin
admin_email = User.objects.all().filter(is_superuser = True)
subject = 'File submitted to QQIresources, awaiting approval'
to_email = admin_email[0].email
from_email = request.user.email
message = 'A file has been submitted to QQIresources by ' + str(request.user) + ' and is awaiting admin approval. \n \n Title: ' + str(fform.title) + '\n Author: ' + str(fform.author) + '\n Description: ' + str(fform.description)
send_mail(subject, message, from_email, [to_email])
return redirect('init')
fileform = FileForm()
return render(request, 'file_form.html', {'fileform': fileform})
我知道變量名是靠近的,但我認爲我正在使用的是正確的Fileform = FileForm(...小寫'f'表單中的第一個和大寫字母'F'第二個。謝謝 –
我會改變它以使它們更加清晰。但這不是問題,謝謝 –