Не могу прочесть файл json для отображения во view layer. Может его в public директорию вынести?
FileNotFoundError at /
[Errno 2] No such file or directory: 'D:\\projects\\django2\\django2cpu.json'
Request Method: GET
Request URL:
127.0.0.1:8000
Django Version: 4.2.4
Exception Type: FileNotFoundError
Exception Value:
[Errno 2] No such file or directory: 'D:\\projects\\django2\\django2cpu.json'
Exception Location: D:\projects\django2\django2\views.py, line 10, in index
Raised during: django2.views.index
Python Executable: C:\Users\flats\AppData\Local\Programs\Python\Python311\python.exe
Python Version: 3.11.2
Python Path:
['D:\\projects\\django2',
'C:\\Users\\flats\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
'C:\\Users\\flats\\AppData\\Local\\Programs\\Python\\Python311\\DLLs',
'C:\\Users\\flats\\AppData\\Local\\Programs\\Python\\Python311\\Lib',
'C:\\Users\\flats\\AppData\\Local\\Programs\\Python\\Python311',
'C:\\Users\\flats\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages']
Server time: Sat, 12 Aug 2023 08:26:18 +0000
from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
import json
import os
@require_http_methods(["GET"])
def index(request):
#return render_to_string('home/index.html', {})
with open(os.path.dirname(os.path.realpath(__file__)) + 'cpu.json', 'r', encoding='utf-8') as fh:
data = json.load(fh)
return render(request, 'home/index.html', {data: data})