我試圖讓 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)
我猜這不是“正確”的做法,但它對我有用。請隨時評論更好的解決方案!
添加回答
舉報
0/150
提交
取消