开发者

Handling file submissions with html and python

开发者 https://www.devze.com 2023-03-24 09:15 出处:网络
I\'m trying to create a form that will handle video file uploads from the user, but I\'m running into a bunch of problems. I am trying to avoid building a model form for this because I won\'t be savin

I'm trying to create a form that will handle video file uploads from the user, but I'm running into a bunch of problems. I am trying to avoid building a model form for this because I won't be saving the file to my database long term. I just want to get it to the server so that I can submit it to youtube. The html I have is:

<form method='post' action='/new/' enctype="multi-part/form-data">{% csrf_token %}
    <input type='file' name='file' id='file'/>
    <input type='submit' />
</form>

and then the view attempts to handle the file like so:

def create_video(request):
    if request.method == 'POST':
        video = request.POST['file']

        command=subprocess.Popen('youtube-upload --email=' + email + ' --password=' + password + '--title=' + title + ' --description=' + description + ' --category=Sports ' + video, stdout=subprocess.PIPE)

        vid = command.stdout.read()

        # do stuff to save video instance to database
        return show_video(request, video.id)
    else:
        form=Video()
    return render_to_response('create_video.html', RequestContext(request, locals()))

note: youtube-upload is a python module to upload videos to y开发者_如何转开发outube with that given command.

So for starters when I submit the form from the front end django sends a message saying "Key 'file' not found in <QueryDict:...

and given that I fix the form so that it will submit properly is the rest of the view properly handling the file?


request.POST doesn't contain file upload information. You need to use request.FILES.

0

精彩评论

暂无评论...
验证码 换一张
取 消