TechAE Blogs - Explore now for new leading-edge technologies

TechAE Blogs - a global platform designed to promote the latest technologies like artificial intelligence, big data analytics, and blockchain.

Full width home advertisement

Post Page Advertisement [Top]

How To Render REST API On Django

How To Render REST API On Django


The process of querying and converting tabular database values into JSON or another format

is called serialization. When you’re creating an API, correct serialization of data is the major challenge.

Here we use serializers to build a REST Django API.

Django Project Setup

Make a folder and open cmd from that folder. Use the following commands step by step

1. To create isolated python environments

python -m venv venv

2. To activate the virtual environment


3. Enter the Django command after you've established and activated a virtual environment

pip install django

4. To make a project directory

django-admin startproject projectname

5. Go to the project directory

cd projectname

6. Create the Django App

python startapp appname

7. Start the server

python runserver

8. Open the project

code .

Build a REST API with Django REST Framework

Create a model in the database that the Django ORM will manage. Run the following commands


1. Start the Django server

python migrate

2. Create Django Django admin

python createsuperuser

python runserver

3. Check admin on

4. Register your app in projectname/



    ... # Leave all the other INSTALLED_APPS


5. Create a Model in

class biodata(models.Model):
    name = models.CharField(max_length=60)
    rollno = models.IntegerField()
    section = models.CharField(max_length=1)
    year = models.IntegerField()

    def __str__(self):

6. Migrate model on Admin

python migrate
python makemigrations
python runserver

7. Register database name in appname/

from .models import biodata

Also, add some data manually to Django admin So that it will be viewed on the front end.

8. Setup Django REST Framework. Run the following command on Terminal

 pip install djangorestframework

9. Adding REST Framework in


    # All your installed apps stay the same




10. Create a file in the appname/ and make a class in it where the bioData model will be serialized

from rest_framework import serializers
from .models import biodata

class Biodataserializers(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = biodata
        fields = ('name', 'rollno', 'section', 'year')

11. Make a class in file after importing the serializer file

from django.shortcuts import render
from .models import biodata
from .serializers import Biodataserializers
from rest_framework import viewsets
import requests

class BiodataViewSet(viewsets.ModelViewSet):
    queryset = biodata.objects.all().order_by('rollno')
    serializer_class =  Biodataserializers  

12. Command to install requests

pip install request

13. Include your app name in the projectname/

from django.urls import include

path( '' , include( 'appname.urls'))

14. Register database in appname/

from django.urls import include, path
from rest_framework import routers
from appname import views

router = routers.DefaultRouter()
router.register(r'biodata', views.BiodataViewSet)

urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),


And here, the rest API is created. Check by using the command

python runserver

Render API on the frontend with Core Django

Now, we have created our REST API, now we want to check it on our frontend.

1. Add def function in file

def biodata(request):
    responsedata = requests.get('').json()    
    return render(request, 'data.html', {'responsedata': responsedata})

2. In App folder make the templates folder and create the file name appname/templates/data.html

{% for i in responsedata %}
<br />
<br />
<br />

{% endfor %}

3. Adding path to appname/

urlpatterns = [
      path('', include(router.urls)),
      path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
      path('data/', views.biodata, name="data"), 

4. View your API on the server by using the command

Thank you for reading!

So we found a clear method to create REST API using Django and then rendering on our frontend.

If you found this article useful, stay tuned for more articles on Django.


No comments:

Post a Comment

Thank you for submitting your comment! We appreciate your feedback and will review it as soon as possible. Please note that all comments are moderated and may take some time to appear on the site. We ask that you please keep your comments respectful and refrain from using offensive language or making personal attacks. Thank you for contributing to the conversation!

Bottom Ad [Post Page]