Database-related actions
model
Use the ORM technology (Object Relational Mapping Object-relational mapping)
- The business logic
解耦合
(object.save () object.delete (), etc.) - Relational database (using DDL, through
models定义
implementation-defined database table.)
MySQL connection driver
-
mysqlclient
- python 2, 3 can be used directly
- Fatal flaw: the installation requirements have mysql, the configuration file must exist in the specified location
-
python-mysql
- python2 good support
- python3 not supported
-
pymysql
- python2, python3 support
- It can also be disguised in front of the library.
Create a database table
We modify the App/models.py
file, the code is as follows:
# models.py
from django.db import models
class Student(models.Model):
s_name = models.CharField(max_length=16) # 设置最大长度16
s_age = models.IntegerField(default=1) # 设置默认值1
Representatives of the class above 数据库表名
, and inherited models.Model, inside the class field represents a field in a data table (name), data type by as CharField (equivalent varchar), DateField (equivalent datetime), max_length parameters define the length .
It is defined in the model and does not generate a database table, required 迁移
.
migrate
The concept of migration: the model is mapped to a database.
(1) generating migration , in 命令行
operation:
$ python manage.py makemigrations ## 生成迁移文件
或
$ python manage.py makemigrations [django的app名]## 针对某一个django模型
Output:
## 运行结果
Migrations for 'two':
two\migrations\0001_initial.py
- Create model Student
The resulting migration of files in the corresponding app in the folder migrations file under:
(2) perform the migration , it is to map the migration of files to the database , that creates a corresponding database table:
$ python manage.py migrate # 创建表结构
Composition table structure: 应用名_类名
(eg: app_test).
Note: Although we do not have the models set the primary key to the table, but Django will automatically add an id as the primary key.
adding data
First, urls.py
add
from django.conf.urls import url
from App import views
urlpatterns = [
url(r'^index/',views.index),
url(r'^addstudent/',views.addStudent) # 添加此项
]
Adding data need to create an object, and then execute the save function, the equivalent of the SQL INSERT, in models.py added in
import random
from django.http import HttpResponse
# Create your views here.
from two.models import Student
def addStudent(request):
stu = Student()
stu.s_name = 'Jerry %d' % random.randrange(100)
stu.s_age = 10
stu.save()
return HttpResponse('添加成功')
Query data
First, urls.py
add
from django.conf.urls import url
from App import views
urlpatterns = [
url(r'^index/',views.index),
url(r'^addstudent/',views.addStudent),
url(r'^getstudent/',views.getstudent), # 添加此项
]
In views.py added in
import random
from django.http import HttpResponse
# Create your views here.
from two.models import Student
def getstudent(request):
students = Student.objects.all()
for student in students:
print(student.s_name)
#return HttpResponse('查找成功')
context = {
"hobby":"Play Games",
"students":students # 查询出来的student_list
}
return render(request,'student_list.html',context)
In templates
the Addstudent_list.html
This uses 模板语法
the for loop, 浏览器
can not be resolved template syntax, django will be converted into html syntax is sent to the browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>StudentList</title>
</head>
<body>
<h1>{{hobby}}</h1> # {{}}读取后端的数据
<ul>
{% for student in students %}
<li>{{student.s_name}}</li> # {{}}读取后端的数据
{% endfor %}
</ul>
</body>
</html>
address:
Update
First, urls.py
add
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^updatestudent/',views.updatestudent)
]
In views.py added in
def updatestudent(request):
student = Student.objects.get(pk=1)
student.s_name = 'Tom'
student.save()
return HttpResponse('更新成功')
Visit the website and view the database:
Deletion
First, urls.py
add
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^deletestudent/',views.updatestudent)
]
In views.py added in
def deletestudent(request):
student = Student.objects.get(pk=2)
student.delete()
return HttpResponse('删除成功')
Access address and query the database: