为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我不能在 django 中使用 pandas 指定媒体文件夹中的文件位置?

为什么我不能在 django 中使用 pandas 指定媒体文件夹中的文件位置?

慕后森 2023-07-27 14:06:44
我试图让 pandas 从媒体文件夹中读取文件作为read_excel(). 这是我尝试过的:from django.shortcuts import renderfrom .models import Benchmarkimport pandas as pd# Create your views here.def main_view(request):    file = pd.read_excel('media/uploads/benchmarks/benchmarks_for_website.xlsx', index_col=0)    context = {        'df': file.to_html()    }    return render(request, 'main.html', context)它可以编译,但是当我尝试在浏览器中访问 html 页面时,出现以下错误:FileNotFoundError at /benchmarks/ [Errno 2] 没有这样的文件或目录:“media/uploads/benchmarks/benchmarks_for_website.xlsx”我究竟做错了什么?另外,我还有MEDIA_ROOT和MEDIA_URL:MEDIA_URL = '/media/'MEDIA_ROOT = '/home/user/project/media'很感谢任何形式的帮助!
查看完整描述

1 回答

?
浮云间

TA贡献1829条经验 获得超4个赞

因此,经过一天的进一步尝试,我决定测试一下将实际的网站网址放在该media位之前。所以最终它看起来是这样的:


from django.shortcuts import render

from .models import Benchmark

import pandas as pd


# Create your views here.


def main_view(request):

    file = pd.read_excel('https://url.com/media/uploads/benchmarks/benchmarks_for_website.xlsx', index_col=0)

    context = {

        'df': file.to_html()

    }

    return render(request, 'main.html', context)

我猜这不是“正确”的做法,但它对我有用。请随时评论更好的解决方案!


查看完整回答
反对 回复 2023-07-27
  • 1 回答
  • 0 关注
  • 63 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信