django

时间:2018-04-26 09:54:06

标签: python django

每当我在django中调用uploaded_file_url时,它都会显示

/ home /的IOError [Errno 2]没有这样的文件或目录:' /media/sample_6bYaPzY.py'

但我手动检查。目录和文件存在

这是我的代码

uploaded_file_url=None
uploaded_file_url2=None
def upload(request):
    if request.method == 'POST':
        #request.FILES['myfile'] and request.FILES["myfile1"]:
        myfile=request.FILES['myfile']
        myfile2=request.FILES["myfile2"]
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        filename2=fs.save(myfile2.name, myfile2)
        global uploaded_file_url
        uploaded_file_url = fs.url(filename)
        global uploaded_file_url2
        uploaded_file_url2 = fs.url(filename2)
        return HttpResponseRedirect('/home/')


        #upload_file = request.FILES['upload_file']
        #data = [row for row in csv.reader(upload_file.read().splitlines())]
    return render(request, 'myapp/upload.html')


def home(request):
    data= open(os.path.join(settings.MEDIA_ROOT, str(uploaded_file_url)),'rb').read()
    //some code here
    return render(request,'myapp/home.html',{"a":a})

设置配置

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

这里我要打开uploaded_file。这里有任何建议。谢谢提前

0 个答案:

没有答案