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
- Run it locally or in production, or share it as a standalone script
- Automatically convert it to a full Django project when you're ready for it to grow
Quickstart
Install nanodjango:
pip install nanodjango
Write your app in single .py
file - for example:
from django.db import models
from nanodjango import Django
app = Django()
@app.admin
class CountLog(models.Model):
# Standard Django model, registered with the admin site
timestamp = models.DateTimeField(auto_now_add=True)
@app.route("/")
def count(request):
# Standard Django function view
CountLog.objects.create()
return f"<p>Number of page loads: {CountLog.objects.count()}</p>"
@app.api.get("/add")
def add(request):
# Django Ninja API support built in
CountLog.objects.create()
return {"count": CountLog.objects.count()}
@app.route("/slow/")
async def slow(request):
import asyncio
await asyncio.sleep(10)
return "Async views supported"
Save that as counter.py
, then set it up and run it:
nanodjango run counter.py
This will create migrations and a database, and run your project in development mode.
- See Command usage for more options
Convert it to a full site
If your project outgrows its single file, you can convert it into a full Django site:
nanodjango counter.py convert path/to/site --name=counter
- See Converting to a full Django project for more information
Share an app
Nanodjango apps are great for sharing examples and prototypes.
Add inline script metadata at the top with your dependencies:
# /// script
# dependencies = ["nanodjango"]
# ///
and call app.run()
at the bottom:
if __name__ == "__main__":
app.run()
Now your app can be run without installing anything, using uv
or pipx
:
# Run with uv
uv run ./script.py
# or with pipx
pipx run ./script.py
You can still manually install dependencies and run the script directly with Python:
pip install nanodjango
python script.py
Run management commands
Anything you would normally do with manage.py
you can do with nanodjango manage
:
nanodjango manage script.py check
nanodjango manage script.py makemigrations script
nanodjango manage script.py runserver 0:8000
Run in production
Run it using nanodjango serve
:
nanodjango serve counter.py
This will use gunicorn, or uvicorn if you have async views.
Alternatively, you can pass the app directly to a WSGI or ASGI server if you prefer:
gunicorn -w 4 counter:app
uvicorn counter:app
- See Command usage for more options
Further reading
For more details, see
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
File details
Details for the file nanodjango-0.9.2.tar.gz
.
File metadata
- Download URL: nanodjango-0.9.2.tar.gz
- Upload date:
- Size: 27.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f45bc495aaccd38b2644d82e07af2b907b479b23632caf829d12779cad164a6 |
|
MD5 | 551d32c02c056f1c37d6147b594f26d6 |
|
BLAKE2b-256 | 81114ef3bdbf8d8f5226b603dfbb20a4a119f6f5396cf380bf7ac74de16aff62 |
File details
Details for the file nanodjango-0.9.2-py3-none-any.whl
.
File metadata
- Download URL: nanodjango-0.9.2-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e32144a56ddef1485da825bfbcfa5222b280d018d6b4cc21dcbd31cf850a376 |
|
MD5 | 73fdf9c060b9a11e849f275b68c4e0c9 |
|
BLAKE2b-256 | 327c2b2dde371b7c1241fecfe462136281e3d88222f1ebea2d2e52cbf22a0ac9 |