Run Django models and views from a single file, and convert it to a full project.
Project description
nanodjango
Write a Django site in a single file, using views, models and admin, then automatically convert it to a full Django project when you're ready for it to grow.
An alternative to Flask (see example below) and FastAPI (see our django-ninja example) - similar simple syntax, but with full access to Django's features such as the ORM, auth and admin site.
Perfect for experiments, prototypes, sharing working code samples, and deploying small production applications.
Quickstart
Install nanodjango:
pip install nanodjango
Create a file counter.py
using Django's standard features, and the @app.route
and @app.admin
decorators to tell nanodjango where your URLs, views and model admin
are:
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):
CountLog.objects.create()
return f"<p>Number of page loads: {CountLog.objects.count()}</p>"
Save that as counter.py
, then set up your database and run it locally with:
nanodjango run counter.py makemigrations counter
nanodjango run counter.py migrate
nanodjango run counter.py createsuperuser
nanodjango run counter.py
It will create your database in a db.sqlite3
file next to your counter.py
, with
the appropriate migrations in migrations/
.
Run it in production using WSGI:
gunicorn -w 4 counter:app
or automatically convert it to a full Django project:
nanodjango counter.py convert /path/to/project --name=myproject
and with a couple of extra
lines, run the
development server as a standalone script using python
, or use pipx run
to run
it and automatically install dependencies to a temporary virtual environment:
# Either
python script.py
# or
pipx run ./script.py
For more details, see
- Getting started
- Tutorial
- Full Documentation
- Changelog
- Examples including how to use nanodjango with Django Ninja
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for nanodjango-0.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d4b70ae9126f26553f28ccd19bd5aaf91bb0949d49a378abd7cea16f756cc12 |
|
MD5 | 81e2be33cb00fcfef939debdd1a008ba |
|
BLAKE2b-256 | 6f52ec52bd5c3acdfff80fd0be51fc1d401c0b70e1dc040d859de681428bb80d |