Django RESTful API Project Setup: A Beginner-Friendly Tutorial
Image by storyset on Freepik

Django RESTful API Project Setup: A Beginner-Friendly Tutorial

Django REST framework is an extension of Django that specializes in building RESTful Web APIs. REST stands for Representational State Transfer; an approach for building web APIs on top of the HTTP protocol. In this blog, we are going to focus on the extra steps you need to take to kickstart your Django REST Framework project.

Taking a Page from Django

Since this is an extension of Django, most of the steps we need to take have already been covered in this article. The areas you'll need to focus on are;

  • Creating the virtual environment
  • Project - install both Django and Django REST Framework; pip install django djangorestframework. After you have created the project, include rest_framework to the INSTALLED_APPS in the settings module
  • Adding the environment variables
  • Configuring the database

Schema

Since we are building a RESTful API, we need to provide a structured definition of data and its organization. This is where a schema comes in - a machine readable document that outlines all the available API endpoints, URLs and HTTP methods. It describes common rules around format for available endpoints, inputs, authentication methods and contact information;

  • Install the package drf-spectacular; pip install drf-spectacular
  • Add drf_spectacular to the INSTALLED_APPS in the settings module
  • Add the REST_FRAMEWORK settings;

# REST Framework settings

REST_FRAMEWORK = {
    "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}        

  • Include the SPECTACULAR_SETTINGS to the settings module - The value of the DESCRIPTION can include HTML content;

# Spectacular settings

SPECTACULAR_SETTINGS = {
    "TITLE": "Project title",
    "DESCRIPTION": "Project description",
    "VERSION": "1.0.0",
    "CONTACT":{
        "name": "Your name",
        "email": "[email protected]",
    },
}        

  • Configure the root URLConf;

from drf_spectacular.views import SpectacularAPIView

url_patterns = [
    # . . .
    path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
    # . . .
]        

Documentation

This is information added to the schema to make it human readable, thus, providing a comprehensive guide on how to use and integrate your RESTful API. The drf-spectacular package comes with built-in support for Redoc and Swagger documentation tools and all you have to do is configure your root URLConf;

from drf_spectacular.views import SpectacularRedocView, SpectacularSwaggerView

url_patterns = [
    # ...
    path("api/schema/swagger-ui/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
    path("api/schema/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"),
    # ...
]        

To provide information on the endpoints that will be listed, include a docstring in their views. Use of class docstrings will lead to that description being applied to every endpoint defined in that class.

Static Files

Even though this is a RESTful API, we need to consider the static files related to the browsable API, admin interface and documentation tools. Therefore, in your settings module in the project directory, add the following;

STATIC_ROOT= BASE_DIR/'staticfiles'

STATICFILES_DIRS= [BASE_DIR/'static']        

Then collect the static files; python manage.py collectstatic

Is Everything Working?

Before you continue, head over to this article in the Final Touch section to add the finishing touches to your RESTful API project. Then, in your root URLConf include the following view and endpoint;

from django.urls import reverse
from django.http import HttpResponsePermanentRedirect

def home(request):
    return HttpResponsePermanentRedirect(reverse("swagger-ui"))

urlpatterns = [
    # . . .
    path("", home, name="home"),
    # . . .
]        

This redirects users to the Swagger documentation when they request the base URL. If you chose the Redoc documentation, change the argument passed to reverse to the name of that endpoint.

Then run your application; python manage.py runserver

Conclusion

Now that you know the LONG version of doing this, you can head over to this template repository and use it when you start working on your next RESTful API project. If you have any questions, please feel free to reach out and HAPPY CODING ??

要查看或添加评论,请登录

社区洞察

其他会员也浏览了