当前位置:网站首页>Django uses mysqlclient service to connect and write to the database
Django uses mysqlclient service to connect and write to the database
2022-07-05 19:13:00 【1024 questions】
Django Use mysqlclient The service connects and writes to the database
Get ready
One 、 install mysqlclient service
Two 、settings.py file
1. Write subapplication to
2. Configuration database
3、 ... and . To configure urls.py file
1. Initialize the urls.py file
2. Sub application of urls.py file
Four . To configure models.py Model class
5、 ... and . To configure views.py View class file
6、 ... and . stay template Editor in chief HTML page [ It's easier ]
7、 ... and . Create and execute portable files [ Please omit this step if you have done it ]
8、 ... and . effect
Nine . Summary
Django Use mysqlclient The service connects and writes to the database Get ready1. establish Django Program , install Django service ( Please see the previous section for details )
2. Create subapplication ( Please see the previous section for details )
3. Connect to the database service , Create a new database , You can also use old data .
stay Terminal Console input :
Two 、settings.py file 1. Write subapplication topip install mysqlclient==2.0
Followed by the version number , Write but not write
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'stu'# The name of the created sub application ]2. Configuration database DATABASES = { 'default': { # Connect local mysql database 'ENGINE': 'django.db.backends.mysql', 'NAME': 'student',# Database name 'USER': 'root',# user name 'PASSWORD': 'root',# password 'HOST': 'localhost',# Local ip 'PORT': 3306,# Port number }} 3、 ... and . To configure urls.py file 1. Initialize the urls.py file from django.contrib import adminfrom django.urls import path, includeurlpatterns = [ path('admin/', admin.site.urls), path('stu/', include('stu.urls'))# Access subapplication ]2. Sub application of urls.py file from django.urls import pathfrom stu import viewsurlpatterns = [ path('gosigt/', views.gosigt),# route path('insigt/', views.insigt),# route ] Four . To configure models.py Model class from django.db import modelsclass Stu(models.Model):# Column names in the table : sname=models.CharField(max_length=10) spwd=models.CharField(max_length=10) # Edit the name of the word table class Meta: db_table='stu' 5、 ... and . To configure views.py View class file from django.http import HttpResponsefrom django.shortcuts import renderfrom stu.models import Studef gosigt(request): return render(request,'sigt.html')def insigt(request): sname=request.POST.get('sname')# Get form content spwd=request.POST.get('spwd') if(sname!="" and spwd!=""): # .save() Write data to the database Stu(sname=sname, spwd=spwd).save() return HttpResponse('<h1> Registered successfully !</h1>') return HttpResponse('<h1> Registration failed !</h1>') 6、 ... and . stay template Editor in chief HTML page [ It's easier ]<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title> register </title></head><body> <form method="post" action="/stu/insigt/"> /*post Method needs to write the following code */ {% csrf_token %} user name :<input type="text" name="sname"><br/> The secret   code :<input type="password" name="spwd"><br/> <input type="submit" value=" register "> </form></body></html> 7、 ... and . Create and execute portable files [ Please omit this step if you have done it ]stay Terminal Console input :
8、 ... and . effectCreate migration file :python manage.py makemigrations
Perform migration file :python manage.py migrate stu
Execute stu Is the name of my sub app

This chapter gives an overview of Django Use... In the framework mysqlclient The service implements the operation of connecting and writing to the database , It's all dry stuff , Need to be familiar with the use .
Where are the deficiencies or better suggestions , Welcome to make complaints about Tucao , There are little friends who don't understand , You can write to me , I will answer them one by one , Thank you for your approval , Thank you for your support !
This is about Django Use mysqlclient This is the end of the article about service connection and writing to the database , More about Django mysqlclient Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !
边栏推荐
- Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
- Overview of video self supervised learning
- 视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
- 数学分析_笔记_第9章:曲线积分与曲面积分
- C# 语言的高级应用
- Get wechat avatar and nickname with uniapp
- China law network joins hands to observe the cloud, and the online system is a full link observable platform
- 彻底理解为什么网络 I/O 会被阻塞?
- EasyCVR电子地图中设备播放器loading样式的居中对齐优化
- Django使用mysqlclient服务连接并写入数据库的操作过程
猜你喜欢

5. Data access - entityframework integration

公司破产后,黑石们来了

cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】

IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系

The road of enterprise digital transformation starts from here

Mariadb root用户及普通用户的密码 重置

#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~

Why can't Bi software do correlation analysis? Take you to analyze

Thoroughly understand why network i/o is blocked?

从外卖点单浅谈伪需求
随机推荐
块编辑器如何选择?印象笔记 Verse、Notion、FlowUs
C# 语言的高级应用
Fuzor 2020軟件安裝包下載及安裝教程
Fuzor 2020软件安装包下载及安装教程
Oracle Chinese sorting Oracle Chinese field sorting
ELK分布式日志分析系统部署(华为云)
A cloud opens a new future of smart transportation
MySQL数据库索引教程(超详细)
Windows Oracle open remote connection Windows Server Oracle open remote connection
Go语言 | 02 for循环及常用函数的使用
Teach you to deal with JS reverse picture camouflage hand in hand
Thoroughly understand why network i/o is blocked?
Word查找红色文字 Word查找颜色字体 Word查找突出格式文本
紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
CDB 实例的启动与关闭
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
手机开户选择哪家券商公司比较好哪家平台更安全
Emqx 5.0 officially released: a single cluster supports 100million mqtt connections
#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
Is the performance evaluation of suppliers in the fastener industry cumbersome? Choose the right tool to easily counter attack!