Skip to main content

Run Django models and views from a single file, and convert it to a full project.

Project description

nanodjango

PyPI Documentation Tests Coverage

  • 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 start counter.py

This will create migrations and a database, and run your project in development mode.

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

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.start()

Now your app can be run without installing anything, using uv or pipx:

# Run with uv
uv start ./script.py
# or with pipx
pipx start ./script.py

You can still manually install dependencies and run the script directly with Python:

pip install nanodjango
python script.py

Run in production

Run it in production using a WSGI server:

gunicorn -w 4 counter:app

or if you have async views, use an ASGI server:

uvicorn counter:app

Further reading

For more details, see

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nanodjango-0.8.0.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

nanodjango-0.8.0-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

Details for the file nanodjango-0.8.0.tar.gz.

File metadata

  • Download URL: nanodjango-0.8.0.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for nanodjango-0.8.0.tar.gz
Algorithm Hash digest
SHA256 a3d898784b672619be36f325030799c8850b906ced8e89d60c7e71aebcf4edb1
MD5 301183e37811272050f1e2c7d4961c45
BLAKE2b-256 aff2ba7c207ee93a50b6feb040d2dccdb3de09d40431c649ebce1c4078af3f00

See more details on using hashes here.

File details

Details for the file nanodjango-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: nanodjango-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for nanodjango-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32bcf04c5fbc907750fef8a1b341686b0970ca199afc885aed9c0a5086d7a334
MD5 c51efe941a65545129a649cd1723571f
BLAKE2b-256 13d3cc9024c42fe272abfc35c7928012a4d009a1078cd87752ab327662f2fa98

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page