pip install djangorestframework
#Add 'rest_framework' to your INSTALLED_APPS setting.
INSTALLED_APPS = [
...
'rest_framework',
]
#If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root urls.py file.
urlpatterns = [
...
path('api-auth/', include('rest_framework.urls'))
]
### 1- install libraries ###
pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support
### 2- add rest framework to installed apps in settings.py ###
INSTALLED_APPS = [
...
'rest_framework',
]
### 3- create serializers.py file (used to process python objects into json format) ###
from rest_framework import serializers
from .models import Drink
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ['id', 'name', 'description']
### 4- in views.py create the view that handle the whole process ###
from django.http import JsonResponse
from .models import Product
from .serializers import ProductSerializer
def getProductsAsJson(request):
products = Product.objects.all()
serializer = ProductSerializer(product, many=True)
return JsonResponse(serializer.data, safe=False)
### 5- you can now add this view in urls.py and try it ###
pip install djangorestframework
#Add 'rest_framework' to your INSTALLED_APPS setting.
INSTALLED_APPS = [
...
'rest_framework',
]
#If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root urls.py file.
urlpatterns = [
...
path('api-auth/', include('rest_framework.urls'))
]
$ pipenv install djangorestframework
# or
pip install djangorestframework
# created by eastcoders swahili
from rest_framework.decorators import api_view
from rest_framework.response import Response
# Create your views here.
@api_view()
def say_hellow(request):
return Response("ok")