0
嘗試在表單集中保存文件字段,但調試返回UploadPdfFormSet對象沒有保存屬性。感覺像處理文件在formset中的文檔是缺乏的。你們可以幫助我嗎?保存表單文件字段失敗
forms.py
from django import forms
class UploadPdf(forms.Form):
docfiles = forms.FileField(label='Browse')
class UploadPdfUrl(forms.Form):
docurl = forms.URLField(label='Address')
views.py
def upload(request):
UploadPdfFormSet = formset_factory(UploadPdf)
if request.method == 'POST':
formsetFile = UploadPdfFormSet(request.POST, request.FILES)
if formsetFile.is_valid():
instances = formsetFile.save
for form in instances:
form.save()
return HttpResponse('yey')
else:
formsetFile = UploadPdfFormSet()
formURL = UploadPdfUrl
return render(request, 'pdfchecker/index.html', {
'formsetFile': formsetFile, 'formURL': formURL,
})
UPDATE: New view.py file which successfully saves file:
def upload(request):
UploadPdfFormSet = formset_factory(UploadPdf)
handleUploadPdfFormSet = modelformset_factory(handle_UploadPdf)
if request.method == 'POST':
formsetFile = UploadPdfFormSet(request.POST, request.FILES)
formsetHandle = handleUploadPdfFormSet(request.POST, request.FILES)
if formsetFile.is_valid():
formsetHandle.save()
return HttpResponse('yey')
else:
formsetFile = UploadPdfFormSet()
formURL = UploadPdfUrl
return render(request, 'pdfchecker/index.html', {
'formsetFile': formsetFile, 'formURL': formURL,
})
好像我沒有真正理解表格。我知道你必須使用模型來保存文件。比方說,我在models.py有這樣的: 從django.db進口車型 類handle_UploadPdf(models.Model): DOCFILE = models.FileField( '' upload_to =) 你知道怎麼做我繼續使用formset?我用普通的形式製作了它,但是當我介紹formset的時候,我猜想所有東西都會變得更難一些。 – Koronag
@Koronag但是有這個模型(handle_UploadPdf)的目的是什麼? – isobolev
我想將文件保存到特定位置。正如你現在所看到的,它只是標記爲「。」,但我真的想要一個特定的URL作爲目的地。 我遵循本指南:http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example 但是,只有一個帳戶的規則形式,而不是formsets。所以我有點困惑。 – Koronag