Skip to content

A single file Django micro project created for demonstration purposes to be used in the same way as other Python frameworks.

License

Notifications You must be signed in to change notification settings

pauloxnet/uDjango

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧬 μDjango (micro Django)

A single file Django micro project created for demonstration purposes to be used in the same way as other Python frameworks.

🏛️ History

I created this code while working on an improvement to Will Vincent's Django Microframework repository, which was itself inspired by a talk that Carlton Gibson gave at DjangoCon US 2019: "Using Django as a Micro-Framework".

Starting from that demonstration code I thought of a Django micro application that could be used in the same way as a minimal application used in other Python frameworks such as Flask or FastAPI.

I presented this code during the first sprint day of DjangoCon US 2023, together with Will Vincent and seeing the appreciation I decided to publish it in this repository.

μDjango presentation during the DjangoCon US 2023 sprints in Durham, North Carolina (USA){loading=lazy}

μDjango presentation during the DjangoCon US 2023 sprints in Durham, North Carolina

💻 Set Up

Python

We need a stable and supported version of Python 3 (tested with Python 3.10-3.13):

$ python3 --version
Python 3.13.0

⚗️ Virtualenv

Creating and activating the virtual environment:

$ python3 -m venv .venv
$ source .venv/bin/activate

🧩 Requirements

Installing the required python packages in the virtual environments:

$ python -m pip install django uvicorn
Collecting django
  Using cached Django-5.1.2-py3-none-any.whl.metadata (4.2 kB)
Collecting uvicorn
  Using cached uvicorn-0.31.1-py3-none-any.whl.metadata (6.6 kB)
Collecting asgiref<4,>=3.8.1 (from django)
  Using cached asgiref-3.8.1-py3-none-any.whl.metadata (9.3 kB)
Collecting sqlparse>=0.3.1 (from django)
  Using cached sqlparse-0.5.1-py3-none-any.whl.metadata (3.9 kB)
Collecting click>=7.0 (from uvicorn)
  Using cached click-8.1.7-py3-none-any.whl.metadata (3.0 kB)
Collecting h11>=0.8 (from uvicorn)
  Using cached h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)
Using cached Django-5.1.2-py3-none-any.whl (8.3 MB)
Using cached uvicorn-0.31.1-py3-none-any.whl (63 kB)
Using cached asgiref-3.8.1-py3-none-any.whl (23 kB)
Using cached click-8.1.7-py3-none-any.whl (97 kB)
Using cached h11-0.14.0-py3-none-any.whl (58 kB)
Using cached sqlparse-0.5.1-py3-none-any.whl (44 kB)
Installing collected packages: sqlparse, h11, click, asgiref, uvicorn, django
Successfully installed asgiref-3.8.1 click-8.1.7 django-5.1.2 h11-0.14.0 sqlparse-0.5.1 uvicorn-0.31.1

🧮 Code

Create a new file called main.py and update it as follows:

from django import conf, http, urls
from django.core.handlers import asgi

conf.settings.configure(ALLOWED_HOSTS="*", ROOT_URLCONF=__name__)

app = asgi.ASGIHandler()


async def root(request):
    return http.JsonResponse({"message": "Hello World"})


urlpatterns = [urls.path("", root)]

🏃 Run

Start the server with uvicorn command.

$ uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [...] using StatReload
INFO:     Started server process [...]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.

🔬 Check it

Open your browser at http://127.0.0.1:8000

You will see the JSON response as:

{ "message": "Hello World" }

⚠️ Disclaimer

This code is for demonstration purposes only and should not be used in production. However, the code is released without any guarantee from the author and no liability can be attributed. Use at your own risk.

💬 Sharing

Here's where the μDjango (micro Django) project was shared online in case you want to re-share it.

🦣 Mastodon

🐦️ Twitter

📣 Django forum

⚖️ License

μDjango is licensed under the BSD 3-Clause License.

👥 Authors

👤 Paolo Melchiorre

About

A single file Django micro project created for demonstration purposes to be used in the same way as other Python frameworks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages