Personal study notes, refer to django official documentation: https://docs.djangoproject.com/zh-hans/3.2/
This article is published on my personal blog: Welcome to https://sunguoqi.com/2021/12/06 /Django_00/
Article directory
1. Create a project
1. Use pycharm to create a project
Select a virtual environment to create a django project, and build a virtual isolation environment for the project to avoid bugs caused by different versions of various dependencies.
2. Activate the virtual environment
cd .\venv\Scripts\
...
activate
2. Install Django
pip install django
Verify that Django was installed successfully!
python -m django --version
3. Create a Django project
cd ..
cd ..
Go back to the project root directory and execute the following command
django-admin startproject mysite
The project file directory is as follows.
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
The skeleton of the project has been built. What the specific files are used for, the official django documentation is very clear.
The outermost mysite/ root directory is just a container for your project, the root directory name has no effect on Django, you can rename it to whatever you like.
manage.py: A command-line tool that lets you manage Django projects in various ways. You can read django-admin and manage.py for all manage.py details.
One level inside the mysite/ directory contains your project, which is a pure Python package. Its name is the Python package name you need to use when you refer to anything inside it. (eg mysite.urls).
mysite/ init.py : An empty file that tells Python that this directory should be considered a Python package. If you are a Python beginner, read more about packages in the official documentation.
mysite/settings.py: The configuration file for the Django project. If you want to know how this file works, see Django configuration for details.
mysite/urls.py: The URL declaration for your Django project, like your site's "directory". Read the URL dispatcher documentation for more information on URLs.
mysite/asgi.py: serves as the entry point for your project running on an ASGI compatible web server. Read How to deploy with ASGI for more details.
mysite/wsgi.py: serves as the entry point for your project running on a WSGI compatible web server. Read How to deploy with WSGI for more details.
Fourth, start the project
cd mysite
Enter the project directory and execute the following command to run the project.
python manage.py runserver
5. Initialize the project configuration
Open the settings.py
file, there are various configuration items for the project.
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
1. Configure the database
1.1, create a new mysql database
Django can run without a database, but in order to better manage data for the project, the MySQL database is used here.
The new database command is as follows.
C:\WINDOWS\system32>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql> create database study_django;
Query OK, 1 row affected (0.10 sec)
mysql>
1.2, modify the configuration
settings.py
Modify line 76 to configure the database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'study_django',
'USER': 'root',
'PASSWORD': '数据库用户密码',
'HOST': '127.0.0.1',
'PORT': '3306'
}
}
1.3, installation dependencies
pip install mysqlclient
2. Migrate database tables
python manage.py migrate
3. Use pycharm to manage the database
6. Create Django super user
python manage.py createsuperuser
PS J:\study_django\mysite> python manage.py createsuperuser
用户名 (leave blank to use 'lenovo'): admin
电子邮件地址: 2516943693@qq.com
Password:
Password (again):
Superuser created successfully.
PS J:\study_django\mysite>
1. Rerun the project
python manage.py runserver
2. Enter the Django background management interface
Enter http://127.0.0.1:8000/admin to log in to the background.