简体   繁体   English

django应用程序在读取json文件python时给出404错误

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

Here is the code: 这是代码:
views.py : 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 : 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 : 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>

The output I am getting is hello on the screen. 我得到的输出在屏幕上hello But in the console log I see this: 但是在控制台日志中,我看到了:

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".

The json file is in the same folder where the html template is kept, that is in the tt folder. json文件位于html模板所在的文件夹中,即tt文件夹中。

Please let me know what might I do to avoid the missing part. 请让我知道如何避免丢失零件。

It's no good putting your JSON into the same folder as the template. 将JSON与模板放在同一文件夹中并没有好处。 Templates aren't accessible via HTTP requests; 无法通过HTTP请求访问模板; they are rendered by views served at URLs. 它们由URL提供的视图呈现。

You should put your JSON into a static directory and access it via STATIC_URL; 您应该将JSON放入静态目录,然后通过STATIC_URL访问它; alternatively you could write a view to serve it like you have for your other templates, but there doesn't seem to be any point doing that. 或者,您可以编写一个视图来像其他模板一样为它提供服务,但是这样做似乎没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM