Django RESTful API Project Setup: A Beginner-Friendly Tutorial
George Mobisa
Software Developer | Frontend ?? | Backend ?? | Full Stack ??- Developing optimized applications to make your life easier
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;
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;
# REST Framework settings
REST_FRAMEWORK = {
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}
# Spectacular settings
SPECTACULAR_SETTINGS = {
"TITLE": "Project title",
"DESCRIPTION": "Project description",
"VERSION": "1.0.0",
"CONTACT":{
"name": "Your name",
"email": "[email protected]",
},
}
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 ??