Django for beginner for windows

Helen Hung / 2024-11-10 / 原文

Set up django project

step 1: create a folder for project and switch to the folder in terminal.

step 2: create a virtual environment:
python -m venv virtual_environment_name

step 3: activate the virtual environment:
virtual_environment_name\Scripts\activate

step 4: install Django under the virtual environment:
pip install Django
note: Django only works when the virtual environment is active.

step 5: create a project in Django:
django-admin startproject project_name .
note: the dot can not be omitted.

step 6: create the database:
python manage.py migrate

step 7: view project:
python manage.py runserver

Start an App

step 1: first activate the virtual environment, then start a app:
python manage.py startapp app_name

step 2: define the model: open projectd_folder\app_name\models.py and define the models here.

step 3: activate the model: open project_folder\project_name\settings.py and add app_name, to the end of INSTALLED_APPS

step 4: migrate the data:
Since we change the data of the project, we migrate the data with two command lines.
python manage.py makemigrations app_name
python manage.py migrate
Whenever we change the data administrated by the project, we change the models.py and migrate the data with the two command line above.

step 5: The Django admin site:
step 5-1: set up a superuser
python manage.py createsuperuser
step 5-2: register models
open project_folder\app_name\admin.py
register models: from app_name.models import model_name and admin.site.register(model_name)