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.
pip install django-ninja
It is not required, but you can also put
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
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 (
PUT, etc). In our
api.py file, let's add in a simple "hello world"
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:
Continue on to Parsing input.