Repository: wsvincent/django-microframework
Branch: main
Commit: e65a7033b527
Files: 6
Total size: 7.2 KB
Directory structure:
gitextract__peoia3g/
├── .gitignore
├── README.md
├── hello_django.py
├── hello_django1.py
├── hello_django2.py
└── requirements.txt
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
.DS_Store
.venv/
__pycache__/
.idea
================================================
FILE: README.md
================================================
# µDjango (Django as a Microframework)
How close can Django get to [Flask's](https://flask.palletsprojects.com/en/3.0.x/quickstart/) five-line "Hello, World!" implementation?
<img src="hello_world.png">
[Carlton Gibson](https://github.com/carltongibson) gave a talk at DjangoCon US 2019, [Using Django as a Micro-Framework](https://www.youtube.com/watch?v=w9cYEovduWI&list=PL2NFhrDSOxgXXUMIGOs8lNe2B-f4pXOX-&index=6&t=0s), where he demonstrated a single file implementation of "Hello, World!" in Django.
This repo demonstrates his original code example and subsequent attempts to display "Hello, World!" in a single file in as few lines of code as possible.
## Set Up
On the command line navigate to a directory, create and activate a new Python virtual environment, and install Django via `pip`.
### Windows _(PowerShell)_
```console
# Windows
$ python -m venv .venv
$ .venv\Scripts\Activate.ps1
(.venv) $ python -m pip install django~=5.0.0
```
### macOS _or_ GNU/Linux
```console
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install django~=5.0.0
```
## Option 1: [Carlton Gibson](https://github.com/carltongibson)
```python
# hello_django1.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.http import HttpResponse
from django.urls import path
settings.configure(
ROOT_URLCONF=__name__,
)
def hello_world(request):
return HttpResponse("Hello, Django!")
urlpatterns = [
path("", hello_world)
]
application = WSGIHandler()
```
Install [Gunicorn](https://gunicorn.org) to run the local server.
```
(.venv) $ python -m pip install gunicorn==22.0.0
```
Start the server.
```
(.venv) $ gunicorn hello_django:application
```
Navigate to [http://127.0.0.1:8000](http://127.0.0.1:8000). To stop the Gunicorn server, use `Ctrl+c` on the command line.
## Option 2: [Peter Baumgartner](https://github.com/ipmb)
Peter offered an update using `execute_from_command_line` to make `python hello_django.py` the equivalent of running Django's `manage.py` command. It also does not need `Gunicorn` to be installed.
```python
# hello_django2.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line # new
from django.http import HttpResponse
from django.urls import path
settings.configure(
ROOT_URLCONF=__name__,
DEBUG=True, # new
)
def hello_world(request):
return HttpResponse("Hello, Django!")
urlpatterns = [
path("", hello_world)
]
application = WSGIHandler()
if __name__ == "__main__": # new
execute_from_command_line()
```
Then start the server with Django's `runserver` command.
```
(.venv) $ python hello_django1.py runserver
```
And navigate to [http://127.0.0.1:8000](http://127.0.0.1:8000).
## Option 3: [Paolo Melchiorre](https://github.com/pauloxnet)
Paolo further decreased the size of the file using `lambda` instead of the function, reduced the memory usage using `ALLOWED_HOSTS` instead of `DEBUG`, and made it possible to use the code with `runserver` or `gunicorn`.
```python
# hello_django2.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line
from django.http import HttpResponse
from django.urls import path
settings.configure(
ALLOWED_HOSTS="*", # new
ROOT_URLCONF=__name__,
)
urlpatterns = [path("", lambda request: HttpResponse("Hello, Django!"))] # new
if __name__ == "__main__":
execute_from_command_line()
else: # new
application = WSGIHandler()
```
### Run
#### `runserver`
Start the server with Django's `runserver` command.
```console
(.venv) $ python hello_django2.py runserver
```
#### `gunicorn`
Install [Gunicorn](https://gunicorn.org) to run the local server.
```
(.venv) $ python -m pip install gunicorn==21.2.0
```
Start the server with the `gunicorn` command.
```console
(.venv) $ gunicorn hello_django2:application
```
### Test
Navigate to [http://127.0.0.1:8000](http://127.0.0.1:8000).
To stop the `runserver` or `gunicorn`, use `Ctrl+c` on the command line.
## Option 3b: [Paolo Melchiorre](https://github.com/pauloxnet/uDjango)
At the DjangoCon US 2023 sprints, Paolo presented a new version of this file that uses ASGI and [uvicorn](https://www.uvicorn.org/) to return the JSON response "Hello World".
Install `uvicorn` along with the existing Django installation.
```
(.venv) $ python -m pip install uvicorn
```
Create a new file called `hello_django3.py` and update it as follows:
```python
# hello_django3.py
from django import conf, http, urls
from django.core.handlers.asgi import ASGIHandler
conf.settings.configure(ALLOWED_HOSTS="*", ROOT_URLCONF=__name__)
app = ASGIHandler()
async def root(request):
return http.JsonResponse({"message": "Hello World"})
urlpatterns = [urls.path("", root)]
```
Start the server with the `uvicorn` command:
```
(.venv) $ uvicorn hello_django3:app --reload
```
Open your browser at `http://127.0.0.1:8000` and the JSON response is:
```
{ "message": "Hello World" }
```
## Option 4: [Andrew Godwin](https://github.com/andrewgodwin/django-singlefile)
In March 2024, Andrew Godwin released a small library that makes it easier to write single-file Django applications in a similar way to how you'd write Flask applications. First, install the library.
```
(.venv) $ python -m pip install django-singlefile
```
Then create a file called `hello_django4.py` with the following code:
```python
# hello_django4.py
from django.http import HttpResponse
from django.singlefile import SingleFileApp
app = SingleFileApp()
@app.path("")
def index(request):
name = request.GET.get("name", "World")
return HttpResponse(f"Hello, {name}!")
if __name__ == "__main__":
app.main()
```
To run the app you can call it from the command line:
```
(.venv) $ python hello_django4.py runserver
```
================================================
FILE: hello_django.py
================================================
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.http import HttpResponse
from django.urls import path
settings.configure(
ROOT_URLCONF=__name__,
)
def hello_world(request):
return HttpResponse("Hello, Django!")
urlpatterns = [
path("", hello_world)
]
application = WSGIHandler()
================================================
FILE: hello_django1.py
================================================
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line # new
from django.http import HttpResponse
from django.urls import path
settings.configure(
ROOT_URLCONF=__name__,
DEBUG=True, # new
)
def hello_world(request):
return HttpResponse("Hello, Django!")
urlpatterns = [path("", hello_world)]
application = WSGIHandler()
if __name__ == "__main__": # new
execute_from_command_line()
================================================
FILE: hello_django2.py
================================================
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line
from django.http import HttpResponse
from django.urls import path
settings.configure(
ALLOWED_HOSTS="*", # new
ROOT_URLCONF=__name__,
)
urlpatterns = [path("", lambda request: HttpResponse("Hello, Django!"))] # new
if __name__ == "__main__":
execute_from_command_line()
else: # new
application = WSGIHandler()
================================================
FILE: requirements.txt
================================================
asgiref==3.7.2
django==4.2.6
sqlparse==0.4.4
gitextract__peoia3g/ ├── .gitignore ├── README.md ├── hello_django.py ├── hello_django1.py ├── hello_django2.py └── requirements.txt
SYMBOL INDEX (2 symbols across 2 files) FILE: hello_django.py function hello_world (line 10) | def hello_world(request): FILE: hello_django1.py function hello_world (line 13) | def hello_world(request):
Condensed preview — 6 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (8K chars).
[
{
"path": ".gitignore",
"chars": 35,
"preview": ".DS_Store\n.venv/\n__pycache__/\n.idea"
},
{
"path": "README.md",
"chars": 5956,
"preview": "# µDjango (Django as a Microframework)\n\nHow close can Django get to [Flask's](https://flask.palletsprojects.com/en/3.0.x"
},
{
"path": "hello_django.py",
"chars": 342,
"preview": "from django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.http import HttpResponse\n"
},
{
"path": "hello_django1.py",
"chars": 496,
"preview": "from django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.core.management import ex"
},
{
"path": "hello_django2.py",
"chars": 476,
"preview": "from django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.core.management import ex"
},
{
"path": "requirements.txt",
"chars": 45,
"preview": "asgiref==3.7.2\ndjango==4.2.6\nsqlparse==0.4.4\n"
}
]
About this extraction
This page contains the full source code of the wsvincent/django-microframework GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 6 files (7.2 KB), approximately 2.1k tokens, and a symbol index with 2 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.