Article directory
One: Django's various pre-steps
Install, create project and APP
Installation: Write it later, there are too many ways
to create a project:
command line input:
django-admin startproject <projectName>
<projectName>是项目名的意思,后文代码行中用<>括起来的内容都以中间英文内容含义为主,可以自己定义
Create a template/application:
enter a created project, and then enter the command line
django-admin startapp <appName>
After creating the app, you need to change the setting INSTALLED_APPS in settings.py, and add your appName as a parameter to the end.
Start Django
python manage.py runserver
Create a super administrator
python manage.py createsuperuser
If you want to use the library that comes with django to implement the user system, you need to complete it before registering the super administrator, otherwise an error may be prompted. It is also very troublesome to solve, not even as convenient as re-creating a project and then controlling-c+v.
Two: mysql database related
First you have to create a target database in mysql yourself.
Link
Change the DATABASES settings in settings.py as follows.
name is your database name.
host is the database IP address,
port is the database port,
user is the username for logging in to the database,
password is the user password for logging in to the database
, and then change __init__.py under the appName folder and add the following two lines. (should have been empty)
Set up the table for the database
The models.py in the appName folder
controls the generation of tables by creating a class that inherits from model.
The class name is the table name. The fields in the class represent the fields (name) in the data table, and the data types include CharField (equivalent to varchar), DateField (equivalent to datetime), ForeignKey (equivalent to foreign key), ManyToManyField (equivalent to many-to-many relationship).
Reference for each parameter
Then enter the following two lines of code on the command line to modify the table
python3 manage.py makemigrations //告知py我们对表结构进行了修改
python3 manage.py migrate //链接数据库,并根据修改创建表结构
After that, there are many more tables in the database, some of which are necessary tables automatically generated by Django, and others are written by users themselves in model.py.
Manage tables with a super administrator
First you definitely need to create a super administrator. on top.
Then register in the admin.py file in the app
from .models import <appName>
class <appName+Admin>(admin.ModelAdmin): #标准的话首字母大写
list_display = ["想要显示的属性","用,链接"]
admin.site.register(<appName>, <appName+Admin>)
It was released first, and the follow-up is a bit troublesome. After writing the project, I have an idea to change it.