{"id":23715105,"url":"https://github.com/yuhexiong/ninja-python-django","last_synced_at":"2026-05-01T18:31:57.214Z","repository":{"id":269931166,"uuid":"908865634","full_name":"yuhexiong/ninja-python-django","owner":"yuhexiong","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-14T08:46:15.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-30T11:56:11.580Z","etag":null,"topics":["backend","django","ninja","python","swagger"],"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/yuhexiong.png","metadata":{"files":{"readme":"README-CH.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":"2024-12-27T06:46:01.000Z","updated_at":"2025-01-14T08:46:18.000Z","dependencies_parsed_at":"2026-03-17T19:05:02.040Z","dependency_job_id":null,"html_url":"https://github.com/yuhexiong/ninja-python-django","commit_stats":null,"previous_names":["yuhexiong/ninja-python-django"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yuhexiong/ninja-python-django","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhexiong%2Fninja-python-django","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhexiong%2Fninja-python-django/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhexiong%2Fninja-python-django/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhexiong%2Fninja-python-django/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuhexiong","download_url":"https://codeload.github.com/yuhexiong/ninja-python-django/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuhexiong%2Fninja-python-django/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32508900,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["backend","django","ninja","python","swagger"],"created_at":"2024-12-30T20:53:47.847Z","updated_at":"2026-05-01T18:31:57.194Z","avatar_url":"https://github.com/yuhexiong.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ninja\n\n展示如何使用 Ninja 管理 Django URL 並自動生成 Swagger 文件。\n\n## Overview\n\n- Language: Python v3.12\n- Web FrameWork: Django v5.1.4\n\n## ENV\n\n\n將 `.env.example` 複製為 `.env`  \n\n```yaml\nDB_NAME=postgres\nDB_USER=postgres\nDB_PASSWORD=password\nDB_HOST=localhost\nDB_PORT=5432\n\nSECRET_KEY=\"django-insecure-\"\nDEBUG=True\n```\n\n## Run\n\n### Migration\n\n```bash\npython manage.py migrate\n```\n\n### Install Module\n\n```bash\npoetry install\n```\n\n\n### Run Server\n\n```bash\npython manage.py runserver\n```\n\n伺服器運行在 `http://localhost:8000`  \nswagger 文件在 `http://localhost:8000/docs`  \n\n## Test\n\n### Linux\n```bash\nDEBUG=False python manage.py test authapp.tests --verbosity=0\n```\n\n### Windows\n\n```bash\n$env:DEBUG=\"False\"; python manage.py test ninjadjangoapp.tests --verbosity=0\n```\n\n## Usage Guide\n\n\n### Params\n在 [app views](./ninjadjangoapp/views.py) 中，將參數設置於 view 函數的第二個參數之後。設定完成後，只需在 `urls` 中對應，便可直接透過變數名稱獲取參數值。\n\n### Request Body\n在 [app schemas](./ninjadjangoapp/schemas.py) 中定義請求體結構，包括：資料型態、是否必填、範例資料。定義完成後，Swagger 會自動生成相關文件。  \n將 Schema 放入 [app views](./ninjadjangoapp/views.py) 中作為 view 函數的第二個參數，自動驗證參數並解析為 Schema。在 view 函數中可以透過 `body.` 的方式存取請求參數。\n\n### Response\n在 [app schemas](./ninjadjangoapp/schemas.py) 中定義回應結構，並在 `urls` 中將回應 Schema 與 API 端點綁定，Swagger 將自動顯示回應格式。\n\n### App Urls\n在 [app urls](./ninjadjangoapp/urls.py) 中創建 `Router` 物件，使用 `api_operation` 方法設定：\n- method：HTTP 方法（GET、POST 等）。\n- path：API 路徑。\n- summary：簡短描述。\n- tags：分類標籤。\n- response：指定回應 Schema。\n- 指定要呼叫的 view function。\n\n### Project Urls\n在 [project urls](./ninjadjangoproject/urls.py) 中創建 `NinjaAPI` 物件，使用 `add_router` 方法將各個 app 的 `Router` 添加進來。可將所有 app 的 API 集中管理，並在 Swagger 中統一顯示。\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuhexiong%2Fninja-python-django","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuhexiong%2Fninja-python-django","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuhexiong%2Fninja-python-django/lists"}