Django表单对django无效
我正在Google云中构建一个Django应用程序,我试图使用django-filetransfers来上传视频数据。
forms.py:
class MontageCreateForm(forms.Form):
title = forms.CharField(label='Montage title',
max_length=1000,
widget=forms.TextInput(attrs={'placeholder': 'Montage Name',
'class': 'form-control',
'aria-describedby': 'basic-addon1'}))
file_one = forms.FileField(label = 'Select files',
help_text = 'max. 42 megabytes',
required = True)
file_two = forms.FileField(label = 'Select files',
help_text = 'max. 42 megabytes',
required = True)
file_three = forms.FileField(label = 'Select files',
help_text = 'max. 42 megabytes',
required = False)
file_four = forms.FileField(label = 'Select files',
help_text = 'max. 42 megabytes',
required = False)
views.py:
def create_montage(request):
if request.method == 'POST':
form = MontageCreateForm(request.POST, request.FILES)
print(request.POST)
print(request.FILES)
print(form.is_valid())
if form.is_valid():
montageElementContainer = MontageElementContainer(title = request.POST['title'])
montageElementContainer.save()
for file in request.FILES.getlist('files'):
montageElement = MontageElement(video_file=file, container=montageElementContainer)
montageElement.save()
contextDict = {}
contextDict['title'] = request.POST['title']
contextDict['montageElementContainer'] = montageElementContainer
contextDict['uid'] = montageElementContainer.auto_increment_id
template = loader.get_template('montage_maker/builder.html')
context = RequestContext(request, contextDict)
return HttpResponse(template.render(context))
else:
upload_url, upload_data = prepare_upload(request, 'montage_maker/create_montage.html')
form = MontageCreateForm()
# Render list page with the documents and the form
return render_to_response(
'montage_maker/create_montage.html',
{'form': form, 'upload_url': upload_url, 'upload_data': upload_data},
context_instance=RequestContext(request)
)
但由于某种原因,form.is_valid()总是返回false。 更奇怪的是,当我查看我在views.py中添加的打印语句的输出时,我发现file_one会根据请求传递给视图.FILES和file_two,file_three和file_four通过request.POST传递给视图。 我不知道是什么原因造成的,但我只能假设它与form.is_valid()失败有关。
我的settings.py文件中包含以下行:
PREPARE_UPLOAD_BACKEND = 'filetransfers.backends.default.prepare_upload'
并且我已将文件传输添加到我安装的应用程序中。 任何人都可以解释这个问题?
链接地址: http://www.djcxy.com/p/56561.html