A single file Django micro project created for demonstration purposes to be used in the same way as other Python frameworks.
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
We need a stable and supported version of Python 3 (tested with Python 3.10-3.13):
$ python3 --version
Python 3.13.0
Creating and activating the virtual environment:
$ python3 -m venv .venv
$ source .venv/bin/activate
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
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)]
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.
Open your browser at http://127.0.0.1:8000
You will see the JSON response as:
{ "message": "Hello World" }
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.
Here's where the μDjango (micro Django) project was shared online in case you want to re-share it.
μDjango is licensed under the BSD 3-Clause License.
- 🌍 Blog: www.paulox.net
- 🐙 Github: @[email protected]
- 🦣 Mastodon: @[email protected]
- 🐦️ Twitter: @[email protected]