Hello everyone, I am the blogger of csdn: lqj_ myself
This is my personal blog homepage:
lqj_My blog_CSDN blog-WeChat applet, front-end, python field blogger lqj_I am good at WeChat applet, front-end, python, etc. https://blog.csdn.net/lbcyllqj?spm=1011.2415 .3001.5343 Bilibili welcome attention:Xiao Miao Develop
Xiaomiao Develop's personal space-Xiaomiao Develop personal homepage-哔哩哔哩Video
This article mainly talks about: Quick start, pythonweb development Django framework
Table of contents
Connect to the database you created
Configuration of classes in the models.py file
The operation of orm on data in the views.py file
database preparation
First create a database.
Note: The encoding format of the database is utf-8 format.
SQL statements:
创建数据库编码为utf8:create database <数据库名> default charset utf8 collate utf8_general_ci;
Connect to the database you created
Configuration in setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbpython', # 数据库名字
'USER': '数据库账号',
'PASSWORD': '数据库密码',
'HOST': '127.0.0.1', # 安装MySQL数据库的机器
'PORT': 3306 # 端口
}
}
And use the orm method to create a data table mentioned in the previous blog to create a data table:
Here we take the Department data table as an example.
Configuration of classes in the models.py file
from django.db import models
class Department(models.Model):
title = models.CharField(max_length=16)
name = models.CharField(max_length=32)
age = models.IntegerField(null=True, blank=True)
The operation of orm on data in the views.py file
First of all, we need to import the class class we wrote in the models.py file
from app01 import models
Create functions to manipulate data. Here we take the orm function as an example (note that when operating data in the table, we must correspond to the key values we declared in the class. If we do not have key values in the class, but we add them below If you operate on the data in the function of deleting, modifying and checking, an error will be reported!)
orm add data
def orm(request):
#新建
models.Department.objects.create(title="销售部", name="lqj", age=20)
models.Department.objects.create(title="IT部", name="lqj1", age=20)
models.Department.objects.create(title="运营部", name="lqj2", age=20)
return HttpResponse('成功')
orm delete data
1. Conditional deletion
Indicates to delete app01_department (indication format created by orm: app file name + table name you want to create )
data with id=1.
def orm(request):
#删除
models.Department.objects.filter(id=1).delete()
return HttpResponse('成功')
2. Delete all
Indicates to delete all the data in the app01_department table.
def orm(request):
#删除
models.Department.objects.all().delete()
return HttpResponse('成功')
Note that orm deleting data only deletes the operable data in the data table, but the index value of id cannot be deleted.
As follows:
Add data again after deleting data (it will start from id=22)
orm get data
1. Conditional acquisition
Here, take the data with id=27 in the data table as an example.
def orm(request):
#查询
data_list = models.Department.objects.filter(id=27).first()
print(data_list.id,data_list.name,data_list.age)
return HttpResponse('成功')
2. Get all
def orm(request):
#查询
data_list = models.Department.objects.all()
for obj in data_list:
print(obj.id,obj.name,obj.age)
return HttpResponse('成功')
orm update data
Updates the data keys of an existing data structure in a table.
1. Modification of conditions
Description: Modify the age in the data table from 19 to 20 (id=27)
2. Modify all
Description: Modify the age in the data table from 19 to 22 (all)
def orm(request):
#更新
models.Department.objects.all().update(age=22)
return HttpResponse('成功')
before fixing
after modification
When the data is updated, for the uncreated data structure in the data table, we need to use the syntax of the previous orm operation data table: