Skip to content
Development

Django REST Part 1 - Building a REST API with Django: The Initial Setup

Learn how to initialize a Django REST Framework project from scratch and create your first functional JSON endpoint.

CodingBowl··1 min read
Photo by Nicola Sagliocco on Unsplash

Learn how to initialize a Django REST Framework project from scratch and create your first functional JSON endpoint.

1. Installation and Project Creation

First, create a virtual environment and install the necessary packages.

`
pip install django djangorestframework
django-admin startproject core .
python manage.py startapp api

`

2. Registering the Framework

Add 'rest_framework' and your new app to the INSTALLED_APPS list in core/settings.py:

`
INSTALLED_APPS = [
...
'rest_framework',
'api',
]

`

3. Creating Your First View

In api/views.py, create a simple function-based view using the @api_view decorator.

`
from rest_framework.decorators import api_view
from rest_framework.response import Response

@api_view(['GET'])
def hello_world(request):
return Response({"message": "DRF is alive!"})

``

4. Mapping the URL

In core/urls.py, link a path to your new view:

`
from django.urls import path
from api import views as api_views

urlpatterns = [
path('api/hello/', api_views.hello_world, name='api-hello-world'),
]

`

5. Running the API

Start the development server:

`python manage.py runserver

`

Open your browser and navigate to the following URL to see your JSON data in the DRF Browsable API interface:

URL: http://127.0.0.1:8000/api/hello/