簡體   English   中英

django應用程序在讀取json文件python時給出404錯誤

[英]django application giving 404 error while reading json file python

這是代碼:
views.py

@csrf_exempt
def post_list(request):
    return render(request, 'blog/post_list.html', {})
@csrf_exempt
def show(request):
    return render(request, 'tt/readjson.html', {})

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list),
    path('test/', views.data_return),
    path('tt/', views.show),
]

readjson.html

<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$.getJSON("log0.json", function (data) {
    $.each(data, function (index, value) {
       console.log(value);
    });
});

</script>

</head>
<body> 
Hello
</body>
</html>

我得到的輸出在屏幕上hello 但是在控制台日志中,我看到了:

jquery-1.10.2.js:8706 GET http://127.0.0.1:8000/tt/log0.json 404 (Not Found)
send @ jquery-1.10.2.js:8706
ajax @ jquery-1.10.2.js:8136
jQuery.(anonymous function) @ jquery-1.10.2.js:8282
getJSON @ jquery-1.10.2.js:8265
(anonymous) @ (index):5
jquery-1.10.2.js:8706 XHR failed loading: GET "http://127.0.0.1:8000/tt/log0.json".

json文件位於html模板所在的文件夾中,即tt文件夾中。

請讓我知道如何避免丟失零件。

將JSON與模板放在同一文件夾中並沒有好處。 無法通過HTTP請求訪問模板; 它們由URL提供的視圖呈現。

您應該將JSON放入靜態目錄,然后通過STATIC_URL訪問它; 或者,您可以編寫一個視圖來像其他模板一樣為它提供服務,但是這樣做似乎沒有任何意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM