Skip to content

Tutorial - First steps

This tutorial shows you how to use most of the features of Django Ninja.

This tutorial assumes that you know at least the basics of the Django framework, such as how to create a project and run it.

Installation

pip install django-ninja

Note

This is not required, but you can also add ninja to INSTALLED_APPS. In that case, the OpenAPI/Swagger UI (or Redoc) will be loaded from the bundled JavaScript package (which is faster) (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 an API

Let's create a module for our API. Create an api.py file in the same 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 ships with a decorator for each HTTP method (GET, POST, PUT, etc.). In our api.py file, let's add a simple "hello world" operation.

from ninja import NinjaAPI

api = NinjaAPI()

@api.get("/hello")
def hello(request):
    return "Hello world"

Now go to the browser and open localhost:8000/api/hello which will return a simple JSON response:

"Hello world"

Success

Continue to the next chapter Handling input parameters.

Comments


本文总阅读量次