Create Django project
Dark horse course
django-admin startproject MyDjango
cd MyDjango
python manage.py startapp index # create application: index
You can use the tree command of cmd to view the directory tree
tree /f
views.py:, receive the request, process it, interact with M and T, and return the response. Define processing function, view function
tests.py: a file that writes test code for unit testing.
admin.py: files related to website background management. To establish the connection between the application and the project, you need to register the application and modify settings Installed in PY_ Apps configuration item
_ init.py: description directory is a Python module
models.py: write the contents of database projects. Each class can be associated with a data table
Run the develop web server command
python manage.py runserver 8002
If no port is set, the default port is 8000
Note: there is no ico icon, so an Error 404 is reported in the log, which has no impact
models.py -- class corresponding to design and table, model class
Model class
Model design class
In models Design model class in PY
Must inherit from models Model class
1) Designing the BookInfo class
2) Designing the HeroInfo class
Models.ForeignKey can establish a one to many relationship between two model classes. When django generates a table, it will create a column in the multi table as a foreign key to establish a one to many relationship between the two tables.
diango. The ORM framework is embedded in the. The ORM framework can correspond classes and data tables. The data tables can be operated only through classes and objects.
Design class: model class.
Another function of ORM is to generate tables in the database according to the designed classes.
The first table is the BookInfo class
1 from django.db import models 2 3 # Create your models here. 4 #Books 5 class BookInfo(models.Model):#Inherit from models In the module Model class 6 #Book model class 7 #Class attribute corresponds to the fields in the table 8 #Book title, CharField The description is a string, max_length Specifies the maximum length of the string 9 btitle=models.CharField(max_length=20) 10 #Date of publication, DateField Description is a date model 11 bpub_date=models.DateField()
1) Generate migration file
Command: Python manage Py makemigrations} migration files are generated from model classes.
2) Perform migration and generate tables
Command: Python manage Py migrate , generate a table based on the migration file
Default format for generating table names:
Application name_ Model class name lowercase
The second table is the HeroInfo class
#Field name format of the table corresponding to the Relationship Attribute: relationship attribute name_ ID (foreign key)
1 from django.db import models 2 3 # Create your models here. 4 #Class I 5 #Books 6 class BookInfo(models.Model):#Inherit from models In the module Model class 7 #Book model class 8 #Class attribute corresponds to the fields in the table 9 #Book title, CharField The description is a string, max_length Specifies the maximum length of the string 10 btitle=models.CharField(max_length=20) 11 #Date of publication, DateField Description is a date model 12 bpub_date=models.DateField() 13 #Multi class 14 #Character class 15 class HeroInfo(models.Model): 16 hname=models.CharField(max_length=20)#name 17 hgender=models.BooleanField(default=False)#Gender, BooleanField The explanation is bool Type, default Specify default values, False Representative male 18 #remarks 19 hcomment=models.CharField(max_length=128) 20 # Relationship attribute, establish one to many relationship between books and things 21 # Field name format of the table corresponding to the Relationship Attribute: relationship attribute name_id(Foreign key) 22 hbook=models.ForeignKey(to="BookInfo",on_delete=models.CASCADE)
If an error is reported, typeerror:__ init__ () missing 1 required positional argument: 'on_ delete'
The problem is models Py, write:
hbook=models.ForeignKey(to="BookInfo",on_delete=models.CASCADE)
python manage.py migrate
Database operation
Table 1
Add, delete, query and modify statements:
Insert statement
Query statement
UPDATE statement
Delete statement
back-stage management
1) Localization
Language and time zone localization
Modify settings py
2) Create administrator
python manage.py createsuperuser
Open the browser and enter the web address http://127.0.0.1:8000/admin
3) Register model class
Admin. Under application Registering model classes in PY
Tell the django framework to generate the corresponding table management page according to the registered model class
b=BookInfo()
str(b)_str_
1 from django.contrib import admin 2 from index.models import BookInfo 3 # Background management related files 4 # Register your models here. 5 #Register model class 6 admin.site.register(BookInfo)
4) Custom management page
The user-defined model management class is generated django on the generated management page