Python: Return pre stored json file in response in Django Rest Framework
Question:
I want to write an API, which on a GET call , returns pre-stored simple json file. This file should be pre-stored in file system. How to do that?
register is app name. static is folder inside register. There I keep stations.json file. register/static/stations.json.
Content of this "stations.json" file should be returned in response.
settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'register/static/')
]
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
views.py:
from django.shortcuts import render
# Create your views here.
from django.contrib.auth.models import User
from .serializers import RegisterSerializer
from rest_framework import generics
from django.http import JsonResponse
from django.conf import settings
import json
class RegisterView(generics.CreateAPIView):
queryset = User.objects.all()
serializer_class = RegisterSerializer
def get_stations(request):
with open(settings.STATICFILES_DIRS[0] + '/stations.json', 'r') as f:
data = json.load(f)
return JsonResponse(data)
urls.py:
from django.urls import path
from register.views import RegisterView
from . import views
urlpatterns = [
path('register/', RegisterView.as_view(), name='auth_register'),
path('stations/', views.get_stations, name='get_stations'),
]
setup/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('api/', include('register.urls')),
]
When I hit GET request from Postman: "http://127.0.0.1:8000/api/stations/",
I get error: 500 Internal server error.
TypeError at /api/stations/
Error:
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE">
<title>TypeError
at /api/stations/</title>
<style type="text/css">
html * {
padding: 0;
margin: 0;
}
Answers:
You need to pass safe=False in return JsonResponse(data) as return JsonResponse(data, safe=False)
I want to write an API, which on a GET call , returns pre-stored simple json file. This file should be pre-stored in file system. How to do that?
register is app name. static is folder inside register. There I keep stations.json file. register/static/stations.json.
Content of this "stations.json" file should be returned in response.
settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'register/static/')
]
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
views.py:
from django.shortcuts import render
# Create your views here.
from django.contrib.auth.models import User
from .serializers import RegisterSerializer
from rest_framework import generics
from django.http import JsonResponse
from django.conf import settings
import json
class RegisterView(generics.CreateAPIView):
queryset = User.objects.all()
serializer_class = RegisterSerializer
def get_stations(request):
with open(settings.STATICFILES_DIRS[0] + '/stations.json', 'r') as f:
data = json.load(f)
return JsonResponse(data)
urls.py:
from django.urls import path
from register.views import RegisterView
from . import views
urlpatterns = [
path('register/', RegisterView.as_view(), name='auth_register'),
path('stations/', views.get_stations, name='get_stations'),
]
setup/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('api/', include('register.urls')),
]
When I hit GET request from Postman: "http://127.0.0.1:8000/api/stations/",
I get error: 500 Internal server error.
TypeError at /api/stations/
Error:
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE">
<title>TypeError
at /api/stations/</title>
<style type="text/css">
html * {
padding: 0;
margin: 0;
}
You need to pass safe=False in return JsonResponse(data) as return JsonResponse(data, safe=False)