Reputation: 7273
Here is the code:
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>
The output I am getting is hello
on the screen. 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.
Please let me know what might I do to avoid the missing part.
Upvotes: 0
Views: 283
Reputation: 599610
It's no good putting your JSON into the same folder as the template. Templates aren't accessible via HTTP requests; they are rendered by views served at URLs.
You should put your JSON into a static directory and access it via 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.
Upvotes: 3