{"id":21581550,"url":"https://github.com/thoughtscript/python_django_2024","last_synced_at":"2026-04-04T20:32:45.453Z","repository":{"id":247482806,"uuid":"817454271","full_name":"Thoughtscript/python_django_2024","owner":"Thoughtscript","description":"Python Django with Backing MySQL DB","archived":false,"fork":false,"pushed_at":"2024-12-29T23:20:58.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-29T23:12:37.274Z","etag":null,"topics":["django","docker-compose","mysql","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Thoughtscript.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-06-19T18:39:26.000Z","updated_at":"2025-05-13T00:40:11.000Z","dependencies_parsed_at":"2025-07-29T22:33:53.872Z","dependency_job_id":"fd840cff-4a5b-4f7d-8a81-980732d49372","html_url":"https://github.com/Thoughtscript/python_django_2024","commit_stats":null,"previous_names":["thoughtscript/python_django_2024"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Thoughtscript/python_django_2024","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thoughtscript%2Fpython_django_2024","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thoughtscript%2Fpython_django_2024/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thoughtscript%2Fpython_django_2024/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thoughtscript%2Fpython_django_2024/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Thoughtscript","download_url":"https://codeload.github.com/Thoughtscript/python_django_2024/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Thoughtscript%2Fpython_django_2024/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31413269,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-04T20:09:54.854Z","status":"ssl_error","status_checked_at":"2026-04-04T20:09:44.350Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","docker-compose","mysql","python"],"created_at":"2024-11-24T14:12:54.023Z","updated_at":"2026-04-04T20:32:45.428Z","avatar_url":"https://github.com/Thoughtscript.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# python_django_2024\n\n[![](https://img.shields.io/badge/Python-3.12.3-yellow.svg)](https://www.python.org/downloads/) [![](https://img.shields.io/badge/Docker-blue.svg)](https://www.docker.com/) [![](https://img.shields.io/badge/Bitnami-MySQL-red.svg)](https://hub.docker.com/r/bitnami/mysql) [![](https://img.shields.io/badge/Django-5.0.6-green.svg)](https://www.djangoproject.com/) \n\nRevisiting Django in 2024.\n\nPrimarily looking at configuration. (Esp. newerish support for async workers.)\n\n\u003e Forewarning: obviously don't use this in Production!\n\n## Use\n\nInitialization:\n\n```bash\npython3 --version\npython3 -m pip install --upgrade pip\npython3 -m pip install Django\ndjango-admin startproject djangoexample\n```\n\nLaunch:\n\n```bash\ndocker-compose up\n\n# If using Docker Compose Engine V2:\ndocker compose up\n```\n\n\u003e When's all done you should see the default Django view: http://127.0.0.1:8000/\n\n## Discussion\n\nSome key topics.\n\n### Config Conventions\n\nMost people seem to move **Application Configuration** into it's own **Module** [like so](./django/djangoexample/config/).\n\n### MySQL Connectors\n\nA lot of examples use PyMySQL but [this article](https://adamj.eu/tech/2020/02/04/how-to-use-pymysql-with-django/) gives great reasons why one should avoid that and just use the out of the box connector and drivers (e.g. - `mysqlclient`).\n\nThe above works using fairly paradigmatic config:\n\n```python\nDATABASES = {\n    'default': {\n        \"ENGINE\": \"django.db.backends.mysql\",\n        'NAME': 'example',\n        'USER': 'example',\n        'PASSWORD': 'example',\n        'HOST': 'mysql',\n        'PORT': '3306',\n    }\n}\n```\n\n### Docker Timing Issues\n\nA lot of folks encounter timing issues especially when dealing with Dockerized Containers:\n\n```bash\n#!/usr/bin/env bash\n\nsleep 60\n\necho \"Migrating DB and running server\"\n\nsleep 15 \u0026\u0026 python3 manage.py makemigrations \u0026\u0026 python3 manage.py migrate \u0026\u0026 python3 manage.py runserver 0.0.0.0:8000 \u0026\n\nwait\n```\n\nDjango will spin up immediately and attempt to `migrate` - the success of those `migrations` determines whether the app will actually serve. \n\n\u003e The easiest and least janky way I've seen to address that is to fork the Bash process and impose timer within the script that way the Container can spin up and initialization before it executes any `migration`. That has many advantages over using like Docker `health_checks` (awkward command-based `test` field, timing, and so on).\n\nWithout the `sleep` command you'd see:\n\n```bash\nMySQLdb.OperationalError: (2002, \"Can't connect to server on 'mysql' (115)\")\n```\n```bash\n MySQLdb.OperationalError: (2002, \"Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)\")\ndjango-1  |\ndjango-1  | The above exception was the direct cause of the following exception:\ndjango-1  |\ndjango-1  | Traceback (most recent call last):\ndjango-1  |   File \"/app/manage.py\", line 22, in \u003cmodule\u003e\ndjango-1  |     main()\ndjango-1  |   File \"/app/manage.py\", line 18, in main\ndjango-1  |     execute_from_command_line(sys.argv)\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py\", line 442, in execute_from_command_line\ndjango-1  |     utility.execute()\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py\", line 436, in execute\ndjango-1  |     self.fetch_command(subcommand).run_from_argv(self.argv)\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/management/base.py\", line 413, in run_from_argv\ndjango-1  |     self.execute(*args, **cmd_options)\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/management/base.py\", line 459, in execute\ndjango-1  |     output = self.handle(*args, **options)\ndjango-1  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/management/base.py\", line 107, in wrapper\ndjango-1  |     res = handle_func(*args, **kwargs)\ndjango-1  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/management/commands/migrate.py\", line 100, in handle\ndjango-1  |     self.check(databases=[database])\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/management/base.py\", line 486, in check\ndjango-1  |     all_issues = checks.run_checks(\ndjango-1  |                  ^^^^^^^^^^^^^^^^^^\ndjango-1  |   File \"/usr/local/lib/python3.12/site-packages/django/core/checks/registry.py\", line 88, in run_checks\ndjango-1  |     new_errors = check(app_configs=app_configs, databases=databases)\ndjango-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n```\nAgain the `sleep` command prevents connection issues and `migration` timing effects.\n\nAlso, make sure to bind the Database and server correctly:\n\n* [Here - `settings.py`](django/djangoexample/config/settings.py)\n* [Here - `run.sh`](django/run.sh)\n\n### Foreign Keys\n\nMany websites have the following kinds of examples:\n\n```python\nclass Employee(models.Model):\n    name=models.CharField(max_length=70)\n    address=models.CharField(max_length=90)\n    department=models.ForeignKey(Department, on_delete=models.CASCADE)\n    # ...\n```\n\nFor example: https://www.freecodecamp.org/news/what-is-one-to-many-relationship-in-django/\n\nBut `ForeignKey.to` requires a `str` per: https://docs.djangoproject.com/en/5.0/ref/models/fields/#foreignkey\n\n### JSON Serialization\n\n`HttpRequest` serialization:\n\n```python\nimport json\n\n# Serialize and access Body JSON from Django HttpRequest\njson_body = json.loads(request.body)\n```\n\n```python\nfrom django.http import JsonResponse\n\n# ...\n\n# Automatically serialize response using newerish JsonRespone (no need for json dumps)\nreturn JsonResponse(...)\n```\n\n## Endpoints\n\n1. `http://localhost:8000/`\n1. `http://localhost:8000/test`\n1. `http://localhost:8000/api/examples`\n1. `http://localhost:8000/api/subexamples`\n\nAlso test minimum viable `POST` Request:\n\n```bash\ncurl -i -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X POST \"http://localhost:8000/api/examples/create\" -d '{\"name\": \"example\"}' --insecure\n```\n\n## Resources and Links\n\n1. https://docs.djangoproject.com/en/5.0/intro/tutorial01/\n2. https://docs.djangoproject.com/en/5.0/ref/django-admin/#runserver\n3. https://medium.com/@omaraamir19966/connect-django-with-mysql-database-f946d0f6f9e3\n4. https://dev.to/pragativerma18/unlocking-performance-a-guide-to-async-support-in-django-2jdj\n5. https://www.geeksforgeeks.org/django-basic-app-model-makemigrations-and-migrate/\n6. https://stackoverflow.com/questions/2428092/creating-a-json-response-using-django-and-python\n7. https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/\n8. https://stackoverflow.com/questions/51577441/how-to-seed-django-project-insert-a-bunch-of-data-into-the-project-for-initi\n9. https://dev.to/pragativerma18/unlocking-performance-a-guide-to-async-support-in-django-2jdj\n10. https://www.uvicorn.org/settings/#application\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoughtscript%2Fpython_django_2024","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthoughtscript%2Fpython_django_2024","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthoughtscript%2Fpython_django_2024/lists"}