Đảm bảo bạn đã cài đặt Poetry và Python, nếu chưa, hãy tham khảo bài viết Poetry | Quản lý gói python cho Django, Flask
B1: Tạo thư mục dự án
mkdir projectfolder && cd projectfolder
B2: Tạo môi trường ảo với Poetry
poetry init
poetry shell
B3: Thêm Django:
poetry add django
poetry install
B4: Cài đặt Django trong thư mục
mkdir app && cd app
django-admin startproject core .
Tạo các app phụ:
python manage.py startapp product
Thêm product
vào INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'product' # <-- new
]
Cuối cùng, chúng ta sẽ có cấu trúc thư mục như sau:
.
├── app
│ ├── core
│ │ ├── __pycache__
│ │ ├── __init__.py
│ │ ├── asgi.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── product
│ │ ├── __pycache__
│ │ ├── migrations
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── models.py
│ │ ├── tests.py
│ │ └── views.py
│ ├── db.sqlite3
│ └── manage.py
├── poetry.lock
└── pyproject.toml
Luu ý: Để chạy các lệnh python, chúng ta cần cd và thư mục
app
B6: Chạy di chuyển
python manage.py migrate
B7: Tạo superuser:
python manage.py createsuperuser
Nhập các thông tin cần thiết
Username (leave blank to use 'datvnn'):
Email address:
Password:
Password (again):
Superuser created successfully.
B8: Chạy django
python manage.py runserver
# hoặc
python manage.py runserver 0.0.0.0:8000
Đầu ra:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
July 20, 2023 - 08:03:48
Django version 4.2.3, using settings 'core.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Trang chủ: http://localhost:8000
Admin: http://localhost:8000/admin