使用html模板中的表单上传django文件

时间:2015-01-29 11:49:07

标签: python django

我有一个django应用程序,它具有文件上传功能。我目前在HTML模板中使用表单,而不是使用django表单。我目前的代码如下:

views.py

def new(request):
    file_path = 'c:\\Users\\Me\\Desktop\\'+request.POST['myFile']
    newFile = DjangoFile(file(file_path))
    obj = MyObj(myFile=newFile)
    obj.save()

根据此代码,我必须指定基本文件路径才能读取它并将其保存在MEDIA_ROOT目录中。有什么办法可以读取上传文件的路径,而不是提供硬编码的基本路径吗?

我的html模板包含以下代码:

<p>My File: <input type="file" name="myFile" class="form-control input-md-4" id="myFile" /></p><br />

0 个答案:

没有答案