Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

Handling file submissions with html and python

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 youtube 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...