Django Rest Framework (DRF) Basic Template Boilerplate Skeleton Project

Creating a Django Rest Framework (DRF) project is a great idea to explore building RESTful APIs with Django. Let’s go through the steps to create a sample Django Rest Framework project:

Personal Software/Tools Recommendation

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

Create a Basic DRF Project

Step 1: Create a Virtual Environment

It’s good practice to work within a virtual environment to isolate your project’s dependencies. Open your terminal (or command prompt) and navigate to the directory where you want to create the Django project. Then, create a virtual environment with the following command:

# 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

Install Django and Django Rest Framework within the activated virtual environment:

pip install django
pip install djangorestframework

Step 3: Create the Django Project

Create a new Django project using a placeholder name “mydrfproject“:

django-admin startproject mydrfproject .

Step 4: Create a Django App

Create a new Django app using a placeholder name “myapp”:

python manage.py startapp myapp

Step 5: Configure the Project Settings

Open the settings.py file inside the mydrfproject folder and update the INSTALLED_APPS list to include Django Rest Framework:

# mydrfproject/settings.py

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

Step 6: Create a Simple API View

In your app directory (myapp), open the views.py file and add a simple API view using Django Rest Framework’s APIView:

# 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

Create a urls.py file in your app directory (myapp) to define the URL patterns for the API view:

# 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

Open the urls.py file in the mydrfproject folder and update it to include the API 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

Start the development server and test the API view:

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!"}.

Now you have a basic Django Rest Framework project that provides a simple API endpoint. You can expand upon this project to explore various features of Django Rest Framework, such as serializers, authentication, permissions, pagination, and more. Enjoy exploring Django Rest Framework and building powerful APIs!

Find this Project on Github

Read More!