Tutorial - First Steps
This tutorial shows you how to use Django Ninja with most of its features.
This tutorial assumes that you know at least some basics of the Django Framework, like how to create a project and run it.
Installation
pip install django-ninja
Note
It is not required, but you can also put ninja
to INSTALLED_APPS
.
In that case the OpenAPI/Swagger UI (or Redoc) will be loaded (faster) from the included JavaScript bundle (otherwise the JavaScript bundle comes from a CDN).
Create a Django project
Start a new Django project (or if you already have an existing Django project, skip to the next step).
django-admin startproject myproject
Create the API
Let's create a module for our API. Create an api.py
file in the same directory location as your Django project's root urls.py
:
from ninja import NinjaAPI
api = NinjaAPI()
Now go to urls.py
and add the following:
from django.contrib import admin
from django.urls import path
from .api import api
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", api.urls),
]
Our first operation
Django Ninja comes with a decorator for each HTTP method (GET
, POST
,
PUT
, etc). In our api.py
file, let's add in a simple "hello world"
operation.
from ninja import NinjaAPI
api = NinjaAPI()
@api.get("/hello")
def hello(request):
return "Hello world"
Now browsing to localhost:8000/api/hello will return a simple JSON response:
"Hello world"
Success
Continue on to Parsing input.