Skip to content

radiac/nanodjango

Repository files navigation

nanodjango

PyPI Documentation Tests Coverage

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.

Perfect for experiments, prototypes, tutorials, and small 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(ADMIN_URL="admin/")

@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 counter.py run makemigrations counter
nanodjango counter.py run migrate
nanodjango counter.py run createsuperuser
nanodjango counter.py run

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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages