(DRF) Django Rest Framework

I personally use and recommend you for use the bellow tools while developing Django Projects:

Step 1: Create a Virtual Environment

# On Windows
python -m venv venv

# On macOS/Linux
python3 -m venv venv

Activate the virtual environment:

# On Windows
venv\Scripts\activate

# On macOS/Linux
source venv/bin/activate

Step 2: Install Django and Django Rest Framework

pip install django
pip install djangorestframework

Step 3: Create the Django Project

django-admin startproject mydrfproject .

Step 4: Create a Django App

python manage.py startapp myapp

Step 5: Configure the Project Settings

# mydrfproject/settings.py

INSTALLED_APPS = [
    # ...
    'rest_framework',
    'myapp',
    # ...
]

Step 6: Create a Simple API View

# myapp/views.py

from rest_framework.views import APIView
from rest_framework.response import Response

class HelloWorldAPIView(APIView):
    def get(self, request):
        return Response({"message": "Hello, world!"})

Step 7: Define URL Patterns

# myapp/urls.py

from django.urls import path
from .views import HelloWorldAPIView

urlpatterns = [
    path('hello/', HelloWorldAPIView.as_view(), name='hello_world'),
]

Step 8: Configure the Project URLs

# mydrfproject/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('myapp.urls')),
]

Step 9: Run the Development Server

python manage.py runserver

Visit http://localhost:8000/api/hello/ in your web browser or use tools like curl or Postman to send GET requests to the API endpoint. You should receive a JSON response: {"message": "Hello, world!"}.

1
Subscribe to my newsletter

Read articles from PRERANA SURYAKANT KARANDE directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

PRERANA SURYAKANT KARANDE
PRERANA SURYAKANT KARANDE

Engineer at Tata communications limited Enthusiastic about python libraries and modules and currently learning and gaining experience by doing some hands-on projects on docker,Jenkins,CI/CD, ansible tools. Also, started learning about AWS cloud and devops