1. Get request and post request
GET request:
1. The browser requests a page
2. The search engine makes a
POST request when retrieving keywords:
1. The browser submits data to the server, such as login/registration, etc.
2. APP in Django:
What is APP? And why use APP?
project --> Project (Old Boys Education University)
APP --> Application (Linux Academy/Python Academy/Big Data Academy/Java Academy)
to facilitate us in a large Django project , manage to achieve different business functions.
1. Command to create APP
1. Command line, enter in the root directory of the Django project:
python3 manage.py startapp app name
3. ORM
ORM:
Advantages:
1. Simple, no need to write SQL statements by yourself
2. High development efficiency
Disadvantages:
1. Remember your special syntax
2. Compared with the SQL statements of the great gods, there must be a gap in execution efficiency
Corresponding relationship of ORM:
class- --> DataTable
Object ---> DataRows
Properties ---> Fields
What ORM can do:
1. Manipulate data tables --> create tables/delete tables/modify tables
Operate the classes in models.py
2. Operate data rows --> Data additions, deletions, changes, and inquiries
cannot create a database, create it yourself database
Detailed steps for using Django's ORM:
1. Create a database yourself
create database database name;
2. Set the relevant configuration for connecting to the database in the Django project (tell Django which database to connect to)
# Database related configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Database type connected
'HOST': '127.0.0.1', # Address of connecting database
'PORT': 3306, # Port
'NAME': "day61" , # database name
'USER': 'root', # user
'PASSWORD': '123456' # password
}
}
3. Tell Django to use pymysql instead of the default MySQLDB to connect to the MySQL database
In the project/__init__.py file, write the following two Sentence:
import pymysql
# Tell Django to use pymysql to replace the default MySQLdb
pymysql.install_as_MySQLdb()
4. Define a class in the models.py file under the app,
This class must inherit from the models.Model class classname(models.Model):
...
5. Execute two commands
1. python3 manage.py makemigrations
2. python3 manage.py migrate
Addition and query of ORM single table:
1. Query
models.UserInfo.objects.all()
2. Add
models.UserInfo.objects.create(name="Zhang San")
1. Create a table
1. Do-it-yourself database creation
create database day61
create table userinfo(id int auto_increment primary key varchar(10) not null ,pwd varchar(18) not null
2. Set the relevant configuration for connecting to the database in the Django project (tell Django which database to connect to)
# Database related configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Connected database type
'HOST': '127.0.0.1', # Connected database address
'PORT' : 3306, # port
'NAME': "day61", # database name
'USER': 'root', # user
'PASSWORD': '123456' # password
}
}
Do the following configuration in the settings folder
DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), #connect database type 'ENGINE':'django.db.backends.mysql', ##Connect to the address of the database 'HOST':'127.0.0.1', ##Name database 'NAME':'day61', #user 'USER': 'root', #password 'PASSWORD':'123456' } }
3. Tell Django to use pymysql instead of the default MySQLDB to connect to the MySQL database
Write code under init file
import pymysql
pymysql.install_as_MySQLdb()
4. Define a class in the models.py file under the app, this class must inherit models.Model
Add under the module file
from django.db import models # Create your models here. #ORM related can only be written in this file, and Django cannot find it in other files. class UserInfo(models.Model): id = models.AutoField(primary_key= True)#Create an auto-incrementing primary key field name =models.CharField(null=False,max_length=32)#Create a name field of type varchar(20) that cannot be null
5. Execute two commands
1. python3 manage.py makemigrations 2. python3 manage.py migrate
View table in mysql database
c
2. Delete the table
Comment out the following statements and execute those two statements
class UserInfo(models.Model): id = models.AutoField(primary_key= True)#Create an auto-incrementing primary key field name =models.CharField(null=False,max_length=32)#Create a name field of type varchar(20) that cannot be null
1. python3 manage.py makemigrations 2. python3 manage.py migrate
is recorded in the small book
3. Change the table
The code that modifies class userinfo is executing two instructions
Fourth, add data
5. List display of form operations
Add data to url in django project
from app01 import views
urlpatterns = [
# url (r '^ yimi /', yimi),
# url(r'^xiaohei/',xiaohei),
url(r'^index/',views.index),
# url(r'^user_list/',user_list)
url(r'^user_list/',views.user_list)
]
Add in the views of app01
from django.shortcuts import HttpResponse, render from app01 import models def user_list(request): # Go to the database to query all users #Use the orm tool to query the database instead of querying it yourself ret = models.UserInfo.objects.all() print(ret[0].id,ret[0].name) #Open the user_list.html file return render(request,'user_list.html',{'user_list':ret})
building an html file
exhibit