Getting started
Installation
Install nanodjango:
pip install nanodjango
Your first app
Create a new file, counter.py
with the following:
from django.db import models
from nanodjango import Django
app = Django()
@app.admin
class CountLog(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)
@app.route("/")
def count(request):
# Standard Django function view
CountLog.objects.create()
return f"<p>Number of requests: {CountLog.objects.count()}</p>"
@app.api.get("/add")
def count(request):
# Django Ninja API
CountLog.objects.create()
return {"count": CountLog.objects.count()}
Now use the run
command to create the migrations, apply them, and run your
project:
nanodjango run counter.py
or you could run each step manually using Django management commands:
Serve it in production using gunicorn:
or automatically convert it to a full Django app:
nanodjango counter.py convert /path/to/site --name=myproject
To learn more about how this works, take a look at the Tutorial.