用html和python处理文件提交 [英] Handling file submissions with html and python

查看:367
本文介绍了用html和python处理文件提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以处理来自用户的视频文件上传的表单,但是我遇到了一些问题。我试图避免为此建立一个模型表单,因为我不会将文件保存到我的数据库长期。我只是想把它交给服务器,以便我可以把它提交给YouTube。我的html是:

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

注意:youtube-upload是一个使用该给定命令将视频上传到youtube的python模块。

note: youtube-upload is a python module to upload videos to youtube with that given command.

所以对于初学者,当我从前端提交表单django发送消息说在< QueryDict:...

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?

推荐答案

请求。 POST 不包含文件上传信息。您需要使用 request.FILES

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

这篇关于用html和python处理文件提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆