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.
Difference between GET and POST in HTTP
http://www.techweb.com.cn/network/system/2016-10-11/2407736.shtml
2. APP in Django:
What is APP? And why use APP?
project --> Project
APP --> Application
is convenient for us to manage and implement different business functions in a large Django project.
1. Commands to create APP
1. Command line, in Django Enter the root directory of the project:
python3 manage.py startapp app name
3. ORM framework
1. Introduction
to ORM Object Relational Mapping (ORM) mode is a technology to solve the mismatch between object-oriented and relational databases. Simply put, ORM automatically persists objects in a program to a relational database by using metadata that describes the mapping between objects and the database. So, how to achieve persistence? A simple solution is to hard-code a separate method for each possible database access operation.
This solution has the following shortcomings:
1. The persistence layer lacks elasticity. Once the business requirements change, the interface of the persistence layer must be modified.
2. The persistence layer is bound to the domain model and the relational database model at the same time. No matter whether the domain model or the relational database model changes, the poison modifies the relevant program code of persistence. , increasing the difficulty of software maintenance.
ORM provides another mode for implementing persistence layer, which uses mapping metadata to describe the object-relational mapping, so that ORM middleware can act as a bridge between the business logic layer and the database layer of any application. Java's typical ORM middleware are: Hibernate, ibatis, speedframework.
ORM's methodology is based on three core principles:
· Simplicity: Model data in its most basic form.
· Communicability: The database structure is documented in a language that anyone can understand.
· Accuracy: Create correctly standardized structures based on the data model.
Second, the concept of ORM
Let's start with O/R. The letter O originates from "Object", while R comes from "Relational". Object and relational databases exist in almost all programs. In the business logic layer and the user interface layer, we are object oriented. When the object information changes, we need to save the object information in the relational database.
When you develop an application (without using O/R Mapping), you may write a lot of code in the data access layer to save, delete, read object information from the database, etc. You write a lot of methods in the DAL to read object data, change state objects, etc. And these codes are always repeated.
The main problem that ORM solves is object-relational mapping. The domain model and the relational model are based on the conceptual model, respectively. Domain model is object-oriented while relational model is relational. In general, a persistent class corresponds to a table, each instance of the class corresponds to a record in the table, and each attribute of the class corresponds to each field of the table.
ORM technical features:
1. Improve the development efficiency. Since ORM can automatically map fields and attributes between Entity objects and Tables in the database, we may actually no longer need a dedicated and huge data access layer.
2. ORM provides a mapping to the database, without direct coding of sql, and can obtain data from the database like an operation object.
Third, the advantages and disadvantages of
ORM The disadvantage of ORM is that it will sacrifice the execution efficiency of the program and will fix the thinking mode.
From the point of view of system structure, the system using ORM is generally a multi-layer system. The more layers of the system, the lower the efficiency. ORM is a completely object-oriented approach, and object-oriented approach will also have a certain impact on performance.
As we develop systems, there are generally performance issues. Performance problems mainly arise from incorrect algorithms and incorrect use of databases. The code generated by ORM is generally unlikely to write very efficient algorithms, and it is more likely to be misused in database applications, mainly in the extraction of persistent objects and data processing. If ORM is used, Programmers are likely to extract all data into memory objects, and then filter and process them, which is prone to performance problems.
When persisting objects, ORMs generally persist all properties, and sometimes, this is not desired.
But ORM is a tool, and tools do solve some repetitive, simple labor. This is undeniable. But we can't expect tools to solve all problems once and for all. Some problems still need special treatment, but the parts that require special treatment should be very few for most systems.
Correspondence of ORM:
class ---> data table
object ---> data row
attribute ---> field
What ORM can do:
1. Operate data tables --> create tables/delete tables/modify tables to
operate 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, which must inherit models.
Model class 类名(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<br>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 database address ' HOST ' : ' 127.0.0.1 ' , # #Database name ' 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_.py 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 varchar(20) type field with a name that cannot be null
5. Execute two commands
python3 manage.py makemigrations python3 manage.py migrate
View table in mysql database
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<br><span style="color: #ff0000;"><strong>recorded in the small book</strong></span><br><br>
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 <strong> ret = models.UserInfo.objects.all() </strong> 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