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, 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 (with django-ninja support built in) - 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):
    # 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 count(request):
    # Django Ninja API
    CountLog.objects.create()
    return {"count": CountLog.objects.count()}

Save that as counter.py, then set up your database and run it locally with:

nanodjango start counter.py

It will create your database in a db.sqlite3 file next to your counter.py, with the appropriate migrations in migrations/, and serve your static and media files. Alternatively you could run each of these commands manually with the run command, eg nanodjango run counter.py runserver 0:8000

Run it in production using WSGI:

gunicorn -w 4 counter:app

or automatically convert it to a full Django project:

nanodjango convert counter.py /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

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.7.1.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

nanodjango-0.7.1-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nanodjango-0.7.1.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for nanodjango-0.7.1.tar.gz
Algorithm Hash digest
SHA256 b10282e3e5642b6ed147f05720802732cc4e4de467b0e5786ccfc7fc3c119b36
MD5 e6774d447eb5ef4e254253b2ca4d5eac
BLAKE2b-256 57d4762aeadf5830cf2f86d2eb54e00d4b61f89700a62f9ccde6f17eb08b9b00

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nanodjango-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for nanodjango-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 caf5efde0f60189058c90ae11134a3cdf641579d5b592399da867478184eaa0d
MD5 4b2b2292ded3eaf284af060bda2faae2
BLAKE2b-256 b275ee2a2c02f998283e53a7f019a9a9731ed13f863e9ff0c7453335b60a0c72

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