{"id":17923520,"url":"https://github.com/kshivendu/dj-flask","last_synced_at":"2026-05-04T21:34:33.483Z","repository":{"id":125953013,"uuid":"451965634","full_name":"KShivendu/dj-flask","owner":"KShivendu","description":"A Proof-of-concept python library that allows you to write middlewares that are compatible with Django as well as Flask","archived":false,"fork":false,"pushed_at":"2022-03-07T08:02:43.000Z","size":19,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-03T09:21:45.620Z","etag":null,"topics":["django","flask","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/KShivendu.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}},"created_at":"2022-01-25T16:59:20.000Z","updated_at":"2023-03-04T07:35:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"bf336454-adca-41dd-be7e-b22048a95987","html_url":"https://github.com/KShivendu/dj-flask","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/KShivendu/dj-flask","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KShivendu%2Fdj-flask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KShivendu%2Fdj-flask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KShivendu%2Fdj-flask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KShivendu%2Fdj-flask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KShivendu","download_url":"https://codeload.github.com/KShivendu/dj-flask/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KShivendu%2Fdj-flask/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260249958,"owners_count":22980763,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","flask","python"],"created_at":"2024-10-28T20:44:25.335Z","updated_at":"2026-05-04T21:34:33.446Z","avatar_url":"https://github.com/KShivendu.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dj-Flask\n\nA simple library that allows you to write middlewares that run in Django and well as Flask.\n\n## Setup\n- Run `cd library \u0026\u0026 python setup.py bdist_wheel` to generate files required for installation\n- Run `pip install -r requirements.txt` to install the library\n\n## Running the examples\n- For Django: `cd dj_sample \u0026\u0026 python manage.py runserver`\n- For Flask: `cd flask_sample \u0026\u0026 python server.py`\n\n## Example\n\n```python\n# middleware.py\n\nfrom dj_flask.middleware import (\n    BaseMiddleWare,\n    CustomRequest,\n    CustomNext,\n    CustomResponse,\n)\n\nclass CommonMiddlware(BaseMiddleWare):\n    def intercept(self, request: CustomRequest, next: CustomNext) -\u003e CustomResponse:\n        if not (request.path == \"/even-or-odd\" and request.method == \"GET\"):\n            return next\n        try:\n            num = int(request.query[\"num\"])\n        except:\n            return CustomResponse(\"Bad request\", status=400)\n        else:\n            json_str = dumps({\"isEven\": True if num % 2 == 0 else False})\n            return CustomResponse(\n                json_str,\n                mimetype=\"application/json\",\n                status=200,\n            )\n```\n\n### For Django:\n```python\n# examples/django/app/settings.py\n\nMIDDLEWARE = [\n    'task.middleware.CommonMiddleware',\n    '...'\n]\n```\n\n### For Flask:\n```python\n# examples/flask/server.py\n\nfrom middleware import CommonMiddlware\nfrom flask import Flask\n\napp = Flask('DemoApp')\n\napp.wsgi_app = CommonMiddlware(app.wsgi_app)\n```\n\n### Endpoints\n\n1. http://localhost:8000/even-or-odd?num=22\n    - The middleware returns `{\"isEven\": true}` because num is even\n2. http://localhost:8000/even-or-odd?num=23\n    - The middleware returns `{\"isEven\": true}` because num is odd\n2. http://localhost:8000/even-or-odd?num=foo\n    - The middleware returns Bad request (400) because num is not an integer.\n3. http://localhost:8000/even-or-odd\n    - The middleware returns Bad request (400) because num is missing.\n4. http://localhost:8000/hello (Middleware passes it to the controller.)\n    - The controller returns \"Hello world\" because the middleware passed the request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkshivendu%2Fdj-flask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkshivendu%2Fdj-flask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkshivendu%2Fdj-flask/lists"}