[
  {
    "path": ".gitignore",
    "content": ".DS_Store\n.venv/\n__pycache__/\n.idea"
  },
  {
    "path": "README.md",
    "content": "# µDjango (Django as a Microframework)\n\nHow close can Django get to [Flask's](https://flask.palletsprojects.com/en/3.0.x/quickstart/) five-line \"Hello, World!\" implementation?\n\n<img src=\"hello_world.png\">\n\n[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.\n\nThis 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.\n\n## Set Up\n\nOn the command line navigate to a directory, create and activate a new Python virtual environment, and install Django via `pip`.\n\n### Windows _(PowerShell)_\n\n```console\n# Windows\n$ python -m venv .venv\n$ .venv\\Scripts\\Activate.ps1\n(.venv) $ python -m pip install django~=5.0.0\n```\n\n### macOS _or_ GNU/Linux\n\n```console\n$ python3 -m venv .venv\n$ source .venv/bin/activate\n(.venv) $ python -m pip install django~=5.0.0\n```\n\n## Option 1: [Carlton Gibson](https://github.com/carltongibson)\n\n```python\n# hello_django1.py\nfrom django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.http import HttpResponse\nfrom django.urls import path\n\nsettings.configure(\n    ROOT_URLCONF=__name__,\n)\n\ndef hello_world(request):\n    return HttpResponse(\"Hello, Django!\")\n\nurlpatterns = [\n    path(\"\", hello_world)\n]\n\napplication = WSGIHandler()\n```\n\nInstall [Gunicorn](https://gunicorn.org) to run the local server.\n\n```\n(.venv) $ python -m pip install gunicorn==22.0.0\n```\n\nStart the server.\n\n```\n(.venv) $ gunicorn hello_django:application\n```\n\nNavigate 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.\n\n## Option 2: [Peter Baumgartner](https://github.com/ipmb) \n\nPeter 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.\n\n```python\n# hello_django2.py\nfrom django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.core.management import execute_from_command_line  # new\nfrom django.http import HttpResponse\nfrom django.urls import path\n\nsettings.configure(\n    ROOT_URLCONF=__name__,\n    DEBUG=True,  # new\n)\n\ndef hello_world(request):\n    return HttpResponse(\"Hello, Django!\")\n\nurlpatterns = [\n    path(\"\", hello_world)\n]\n\napplication = WSGIHandler()\n\nif __name__ == \"__main__\":  # new\n    execute_from_command_line()\n```\n\nThen start the server with Django's `runserver` command.\n\n```\n(.venv) $ python hello_django1.py runserver\n```\n\nAnd navigate to [http://127.0.0.1:8000](http://127.0.0.1:8000). \n\n## Option 3: [Paolo Melchiorre](https://github.com/pauloxnet) \n\nPaolo 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`.\n\n```python\n# hello_django2.py\nfrom django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.core.management import execute_from_command_line\nfrom django.http import HttpResponse\nfrom django.urls import path\n\nsettings.configure(\n    ALLOWED_HOSTS=\"*\",  # new\n    ROOT_URLCONF=__name__,\n)\n\nurlpatterns = [path(\"\", lambda request: HttpResponse(\"Hello, Django!\"))]  # new\n\nif __name__ == \"__main__\":\n    execute_from_command_line()\nelse:  # new\n    application = WSGIHandler()\n```\n\n### Run\n\n#### `runserver`\n\nStart the server with Django's `runserver` command.\n\n```console\n(.venv) $ python hello_django2.py runserver\n```\n\n#### `gunicorn`\n\nInstall [Gunicorn](https://gunicorn.org) to run the local server.\n\n```\n(.venv) $ python -m pip install gunicorn==21.2.0\n```\n\nStart the server with the `gunicorn` command.\n\n```console\n(.venv) $ gunicorn hello_django2:application\n```\n\n### Test\n\nNavigate to [http://127.0.0.1:8000](http://127.0.0.1:8000).\n\nTo stop the `runserver` or `gunicorn`, use `Ctrl+c` on the command line.\n\n## Option 3b: [Paolo Melchiorre](https://github.com/pauloxnet/uDjango)\n\nAt 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\".\n\nInstall `uvicorn` along with the existing Django installation.\n\n```\n(.venv) $ python -m pip install uvicorn\n```\n\nCreate a new file called `hello_django3.py` and update it as follows:\n\n```python\n# hello_django3.py\nfrom django import conf, http, urls\nfrom django.core.handlers.asgi import ASGIHandler\n\nconf.settings.configure(ALLOWED_HOSTS=\"*\", ROOT_URLCONF=__name__)\n\napp = ASGIHandler()\n\n\nasync def root(request):\n    return http.JsonResponse({\"message\": \"Hello World\"})\n\n\nurlpatterns = [urls.path(\"\", root)]\n```\n\nStart the server with the `uvicorn` command:\n\n```\n(.venv) $ uvicorn hello_django3:app --reload\n```\n\nOpen your browser at `http://127.0.0.1:8000` and the JSON response is:\n\n```\n{ \"message\": \"Hello World\" }\n```\n\n\n## Option 4: [Andrew Godwin](https://github.com/andrewgodwin/django-singlefile) \n\nIn 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.\n\n```\n(.venv) $ python -m pip install django-singlefile\n```\n\nThen create a file called `hello_django4.py` with the following code:\n\n```python\n# hello_django4.py\nfrom django.http import HttpResponse\nfrom django.singlefile import SingleFileApp\n\napp = SingleFileApp()\n\n\n@app.path(\"\")\ndef index(request):\n    name = request.GET.get(\"name\", \"World\")\n    return HttpResponse(f\"Hello, {name}!\")\n\n\nif __name__ == \"__main__\":\n    app.main()\n```\n\nTo run the app you can call it from the command line:\n\n```\n(.venv) $ python hello_django4.py runserver\n```\n"
  },
  {
    "path": "hello_django.py",
    "content": "from django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.http import HttpResponse\nfrom django.urls import path\n\nsettings.configure(\n    ROOT_URLCONF=__name__,\n)\n\ndef hello_world(request):\n    return HttpResponse(\"Hello, Django!\")\n\nurlpatterns = [\n    path(\"\", hello_world)\n]\n\napplication = WSGIHandler()\n"
  },
  {
    "path": "hello_django1.py",
    "content": "from django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.core.management import execute_from_command_line  # new\nfrom django.http import HttpResponse\nfrom django.urls import path\n\nsettings.configure(\n    ROOT_URLCONF=__name__,\n    DEBUG=True,  # new\n)\n\n\ndef hello_world(request):\n    return HttpResponse(\"Hello, Django!\")\n\n\nurlpatterns = [path(\"\", hello_world)]\n\napplication = WSGIHandler()\n\nif __name__ == \"__main__\":  # new\n    execute_from_command_line()\n"
  },
  {
    "path": "hello_django2.py",
    "content": "from django.conf import settings\nfrom django.core.handlers.wsgi import WSGIHandler\nfrom django.core.management import execute_from_command_line\nfrom django.http import HttpResponse\nfrom django.urls import path\n\nsettings.configure(\n    ALLOWED_HOSTS=\"*\",  # new\n    ROOT_URLCONF=__name__,\n)\n\nurlpatterns = [path(\"\", lambda request: HttpResponse(\"Hello, Django!\"))]  # new\n\nif __name__ == \"__main__\":\n    execute_from_command_line()\nelse:  # new\n    application = WSGIHandler()\n"
  },
  {
    "path": "requirements.txt",
    "content": "asgiref==3.7.2\ndjango==4.2.6\nsqlparse==0.4.4\n"
  }
]