{"id":20619731,"url":"https://github.com/twtrubiks/drf-datatable-example-server-side","last_synced_at":"2025-04-15T12:02:26.850Z","repository":{"id":84518944,"uuid":"91163424","full_name":"twtrubiks/DRF-dataTable-Example-server-side","owner":"twtrubiks","description":"DataTables Example (server-side) - Python Django REST framework","archived":false,"fork":false,"pushed_at":"2019-08-28T11:00:17.000Z","size":2149,"stargazers_count":96,"open_issues_count":2,"forks_count":40,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-28T19:53:44.327Z","etag":null,"topics":["datatables","django","django-rest-framework","server-side"],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twtrubiks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2017-05-13T09:28:01.000Z","updated_at":"2024-07-06T12:35:03.000Z","dependencies_parsed_at":"2023-03-02T04:30:27.263Z","dependency_job_id":null,"html_url":"https://github.com/twtrubiks/DRF-dataTable-Example-server-side","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDRF-dataTable-Example-server-side","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDRF-dataTable-Example-server-side/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDRF-dataTable-Example-server-side/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2FDRF-dataTable-Example-server-side/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/DRF-dataTable-Example-server-side/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067779,"owners_count":21207395,"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":["datatables","django","django-rest-framework","server-side"],"created_at":"2024-11-16T12:12:23.157Z","updated_at":"2025-04-15T12:02:26.844Z","avatar_url":"https://github.com/twtrubiks.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DRF-dataTable-Example-server-side\n\n此版本為 django\u003c2.0，以及搭配 SQLite 的範例，\n\n如果使用 Django\u003e2.0，請參考 [django_2_and_mysql_5.7 branch](https://github.com/twtrubiks/DRF-dataTable-Example-server-side/tree/django_2_and_mysql_5.7)\n\nDataTables Example (server-side) - Python Django REST framework\n\n* [Youtube Demo](https://youtu.be/E0Pf5Ci-vGw) - 建議看影片的 demo 說明\n\nDataTables (server-side) 搭配 [Django REST framework](http://www.django-rest-framework.org/) 簡單範例 📝\n\n## 特色\n\n* 使用 [Django REST framework](http://www.django-rest-framework.org/) 建立API。\n* 搭配 [DataTables]( https://datatables.net/ ) 並且使用 [server-side]( https://datatables.net/manual/server-side ) 增加使用者體驗。\n* 搭配 Bootstrap 。\n* 搭配 SQLite ( 10萬筆模擬資料 )。\n\n## 安裝套件\n\n請在你的命令提示字元 (cmd ) 底下輸入\n\n安裝 [Django](https://github.com/django/django)\n\n這邊請注意，django\u003c2.0 ( django 請安裝小於 2.0 的版本)\n\n\u003epip install Django==1.11.20\n\n安裝 [Django-REST-framework](http://www.django-rest-framework.org/)\n\u003epip install djangorestframework\n\n安裝 [django-model-utils](https://django-model-utils.readthedocs.io/en/latest/index.html)\n\u003epip install django-model-utils\n\n或是使用 cmd (命令提示字元)\n\n```cmd\npip install -r requirements.txt\n```\n\n## 說明\n\n* LOG 資訊非常重要，可以參考官網 [django logging](https://docs.djangoproject.com/en/1.11/topics/logging/) , 或是看範例裡面的  [settings.py](https://github.com/twtrubiks/DRF-dataTable-Example-server-side/blob/master/drf_table_ex/settings.py) 裡面的 LOGGING。\n\n* 建議透過 [django-db-backends](https://docs.djangoproject.com/en/1.11/topics/logging/#django-db-backends) 來觀看目前的 orm 到底執行了什麼 SQL 指令，可以到範例裡面的 [settings.py](https://github.com/twtrubiks/DRF-dataTable-Example-server-side/blob/master/drf_table_ex/settings.py) 找 django.db.backends。\n\n## 執行方法\n\n使用命令提示字元 ( cmd ) 輸入下方指令\n\n\u003e python manage.py runserver\n\n然後瀏覽\n\n[http://127.0.0.1:8000/index/](http://127.0.0.1:8000/index/)\n\n## 執行畫面\n\n### 首頁\n\n#### [Get] api/music/{額外 DataTables 參數}\n\n![alt tag](http://i.imgur.com/PaYzAU4.jpg)\n\n### 新增\n\n#### [POST] api/music/\n\n![alt tag](http://i.imgur.com/fwOxMwr.jpg)\n\n### 編輯\n\n#### [PUT] api/music/{id}/\n\n![alt tag](http://i.imgur.com/3MOF4ud.jpg)\n\n### 刪除\n\n#### [DELETE] api/music/{id}/\n\n![alt tag](http://i.imgur.com/s48Tl6S.jpg)\n\n### 搜尋  排序\n\n#### [Get] api/music/{ 額外 DataTables 參數 }\n\n![alt tag](http://i.imgur.com/Ndvm3bu.jpg)\n\n## 後記\n\n* 從影片中的 demo 可以很明顯的看出當資料量很大（5 萬筆）的時候，如果沒有用 server-side 的方式，而是一次全部載入，使用者體驗非常差。\n\n* 本範例前後端並沒有分離，而是寫在一起，比較好的方式，應該是前後端分離。\n\n* api document 可參考 [api_document.html](https://github.com/twtrubiks/DRF-dataTable-Example-server-side/blob/master/api_document.html) ，直接用瀏覽器開啟即可。 ( 文件教學可參考  [aglio_tutorial](https://github.com/twtrubiks/aglio_tutorial) )\n\n![](http://i.imgur.com/xOe8qsD.png)\n\n## 更換 MySQL 資料庫\n\n因為有些人反映換成 MySQL 之後無法 work，所以我測試了一下，\n\n步驟一，先要多安裝兩個套件，\n\n安裝 PyMySQL\n\n```cmd\npip install PyMySQL\n```\n\n安裝 mysqlclient\n\n```cmd\npip install mysqlclient\n```\n\n( 這個有可能安裝不起來，我自己最後是去找 mysqlclient-1.4.2-cp36-cp36m-win32.whl 安裝成功 )\n\n[settings.py](https://github.com/twtrubiks/DRF-dataTable-Example-server-side/blob/master/drf_table_ex/settings.py#L84) 要改成\n\n```python\n......\n\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.mysql',\n        'NAME': 'demo',\n        'USER': 'root',\n        'PASSWORD': 'password123',\n        'HOST': '127.0.0.1',\n        'PORT': '3306',\n    }\n}\n\n......\n```\n\n也可參考之前的文章 [Django 如何連結 MySQL](https://github.com/twtrubiks/django-transactions-tutorial#django-%E5%A6%82%E4%BD%95%E9%80%A3%E7%B5%90-mysql)，\n\n這邊要特別注意，MySQL 我使用 5.7，如果使用 MySQL 8.0，會遇到 django.db.utils.OperationalError: (2059, )\n\n的問題，原因是 MySQL 8.0 的密碼加密方式改成了 caching_sha2_password，要再自行修改，簡單一點就是使用\n\nMySQL 5.7。\n\n## 執行環境\n\n* Python 3.6.6\n\n## Reference\n\n* [Django REST framework](http://www.django-rest-framework.org/)\n* [DataTables]( https://datatables.net/ )\n* [Bootstrap]( http://getbootstrap.com/ )\n* [django-model-utils](https://django-model-utils.readthedocs.io/en/latest/index.html)\n\n## Donation\n\n文章都是我自己研究內化後原創，如果有幫助到您，也想鼓勵我的話，歡迎請我喝一杯咖啡:laughing:\n\n![alt tag](https://i.imgur.com/LRct9xa.png)\n\n[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8)\n\n## License\n\nMIT license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdrf-datatable-example-server-side","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fdrf-datatable-example-server-side","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdrf-datatable-example-server-side/lists"}