avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1582

Abstract

div><p id="cf04">Next, create a new app within the project:</p><div id="47e7"><pre><span class="hljs-keyword">python</span> manage.<span class="hljs-keyword">py</span> startapp <span class="hljs-symbol"><app_name></span></pre></div><p id="115f">Add the newly created app to the <code>INSTALLED_APPS</code> list in the <code>settings.py</code> file.</p><h2 id="218f">Creating a Basic API</h2><p id="a01e">Inside the newly created app, let’s create a file called <code>api.py</code> 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.</p><div id="0f23"><pre><span class="hljs-keyword">from</span> ninja <span class="hljs-keyword">import</span> NinjaAPI

api = NinjaAPI()

<span class="hljs-meta">@api.get(<span class="hljs-params"><span class="hljs-string">"/home"</span></span>)</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">home</span>(<span class="hljs-params">request</span>): <span class="hljs-keyword">return</span> {<span class="hljs-string">"message"</span>: <span class="hljs-string">"Hello, World"</span>}</pre></div><h2 id="4678">Configuring URL Routing</h2><p id="f2d0">Now, in the main <code>urls.py</code> file of the project, we need to define the path for our Ninja API:</p><div id="101e"><pre><span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> <span class="hljs-type">path</span> <span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> <span class="hljs-keyword">inclu

Options

de</span>

<span class="hljs-keyword">from</span> app_name.api <span class="hljs-keyword">import</span> api

urlpatterns = [ path("api/", <span class="hljs-keyword">include</span>(api.urls)), ]</pre></div><h2 id="5b2d">Making HTTP Requests</h2><p id="06fd">With the API set up, let’s make a <code>GET</code> request to our "Hello, World" endpoint using <code>curl</code>:</p><div id="fec5"><pre><span class="hljs-symbol">curl</span> -X <span class="hljs-meta">GET</span> http:<span class="hljs-comment">//localhost:8000/api/home</span></pre></div><p id="ceb0">This should return a JSON response with the message “Hello, World”.</p><div id="1731"><pre><span class="hljs-punctuation">{</span><span class="hljs-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Hello, World"</span><span class="hljs-punctuation">}</span></pre></div><h2 id="a118">Conclusion</h2><p id="6a74">In this tutorial, we learned the basic setup of a Django Ninja API and how to make HTTP requests using <code>curl</code>. 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.</p><figure id="1748"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*4zPWUgr1qeyKNNkp.jpeg"><figcaption></figcaption></figure><p id="9de0"><a href="https://readmedium.com/langchain-multi-modal-rag-template-7d37c6045588">LANGCHAIN — Multi-Modal RAG Template</a></p></article></body>

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-ninja

Now, 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/home

This 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.

LANGCHAIN — Multi-Modal RAG Template

Django
Ninja
ChatGPT
Python
Recommended from ReadMedium