Cấu trúc thư mục yêu thích hiện khi bắt đầu một project Django: tách settings
, apps
, … thành các module riêng. Quan điểm cá nhân sau khi thực hiện theo cấu trúc này thì:
- Phân biệt các cấu hình giữa môi trường (development, production, test)
- Dễ dàng tùy chỉnh các apps cần dùng cho môi trường development như
django-debug-toolbar
Còn nhược điểm thì …, để sau.
Cấu trúc của project sau khi thực thi lênh django-admin startproject
và django-admin startapp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
project
| manage.py
| backend
| __init__.py
| asgi.py
| settings.py
| wsgi.py
| urls.py
| app
| __init__.py
| admin.py
| apps.py
| models.py
| views.py
| tests.py
| requirements.txt
Ta sẽ tiến hành thay đổi cấu trúc như sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
project
| manage.py
| backend
| __init__.py
| asgi.py
| apps
| api
| __init__.py
| admin.py
| apps.py
| models.py
| views.py
| tests
| __init__.py
| views.py
| models.py
| settings
| __init__.py
| base.py
| development.py
| production.py
| wsgi.py
| urls.py
| requirements
| base.txt
| development.txt
| production.txt
| test.txt
Sau đó thì có vài điều cần chỉnh sửa lại:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Các chỉnh sửa cho file cấu hình base.py
- BASE_DIR = Path(__file__).resolve().parent.parent.parent
+ BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
INSTALLED_APPS = [
...
- "api"
+ "backend.apps.api"
]
Khi startapp thì chỉnh sửa apps.py
class ApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
- name = "api"
+ name = "backend.apps.api"
urlpatterns = [
- path("api/", include("api.urls")),
+ path("api/", include("backend.apps.api.urls"))
]