我有这个功能:
def postcodes(request):
data = open('stores.json').read()
jsonData = json.dumps(data)
这是我的目录结构:
âââ core
â  âââ admin.py
â  âââ apps.py
â  âââ forms.py
â  âââ __init__.py
â  âââ migrations
â  â  âââ __init__.py
â  âââ models.py
â  âââ stores.json
â  âââ templates
â  â  âââ core
â  â  âââ github.html
â  â  âââ home.html
â  â  âââ oxford.html
â  â  âââ stores.html
â  âââ tests.py
â  âââ urls.py
â  âââ views.py
功能在我的
views.py
文件和文件
stores.json
在同一个目录中。
这是回溯:
Internal Server Error: /stores/
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/kristian/.virtualenvs/rest_tails2/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kristian/tails/restful-apis-example/core/views.py", line 83, in postcodes
data = open('stores.json').read() #opens the json file and saves the raw contents
FileNotFoundError: [Errno 2] No such file or directory: 'stores.json'
[25/Nov/2018 20:07:31] "GET /stores/ HTTP/1.1" 500 75613
我正考虑宣布
BASE_DIR
对于静态或模板,但我不确定这种方法。
我从未将本地JSON文件加载到Django中,所以对此有什么想法吗?