Django form is not valid with django

I'm building a Django application in Google's cloud and I'm attempting to use django-filetransfers to upload video data.

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)
    )

But for some reason, form.is_valid() always seems to return false. Even stranger, when I look at the output of the print statements that I've added in views.py I see that file_one gets passed to the view on request.FILES and file_two, file_three and file_four get passed to the view on request.POST. I have no idea what's causing this but I can only assume that it's related to form.is_valid() failing.

My settings.py file has the following line in it:

PREPARE_UPLOAD_BACKEND = 'filetransfers.backends.default.prepare_upload'

and I've added filetransfers to my installed apps. Can anyone shed any light on this issue?

链接地址: http://www.djcxy.com/p/56562.html

上一篇: 立即检测客户端与服务器套接字的连接

下一篇: Django表单对django无效