
PYTHON — Django Ninja Python
Measuring programming progress by lines of code is like measuring aircraft building progress by weight. — Bill Gates

LANGCHAIN — What Is DataHerald?
Django Ninja is a library that provides view decorators for each of the HTTP operations used in REST actions. These decorated views are registered for the appropriate REST calls and automatically serialize and deserialize data into JSON format. In this tutorial, we will learn how to set up a basic Django Ninja API and make HTTP requests using curl.
Setting Up a Django Project
Assuming you have a basic understanding of Django, let’s start by installing the necessary libraries and creating a new Django project.
First, install Django Ninja using pip:
pip install django-ninjaNow, use the django-admin command to create a new Django project:
django-admin startproject <project_name>Next, create a new app within the project:
python manage.py startapp <app_name>Add the newly created app to the INSTALLED_APPS list in the settings.py file.
Creating a Basic API
Inside the newly created app, let’s create a file called api.py to define our Ninja-based API. In this file, we'll define a Ninja API using a router and create a simple "Hello, World" endpoint.
from ninja import NinjaAPI
api = NinjaAPI()
@api.get("/home")
def home(request):
return {"message": "Hello, World"}Configuring URL Routing
Now, in the main urls.py file of the project, we need to define the path for our Ninja API:
from django.urls import path
from django.urls import include
from app_name.api import api
urlpatterns = [
path("api/", include(api.urls)),
]Making HTTP Requests
With the API set up, let’s make a GET request to our "Hello, World" endpoint using curl:
curl -X GET http://localhost:8000/api/homeThis should return a JSON response with the message “Hello, World”.
{"message": "Hello, World"}Conclusion
In this tutorial, we learned the basic setup of a Django Ninja API and how to make HTTP requests using curl. Django Ninja provides a convenient way to create RESTful APIs with minimal boilerplate code. You can now further explore advanced features such as URL arguments, query strings, serialization, authentication, and error management with Django Ninja.

