当前位置:网站首页>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 !
边栏推荐
- CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]
- Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
- 跨境支付平台 XTransfer 的低代码实践:如何与其他中台融合是核心
- C# 语言的高级应用
- Interprocess communication (IPC): shared memory
- 5. 数据访问 - EntityFramework集成
- The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- 华为让出的高端市场,小米12S靠徕卡能抢到吗?
- Redhat7.4 configure Yum software warehouse (rhel7.4)
猜你喜欢
Decision tree and random forest
Teach you to deal with JS reverse picture camouflage hand in hand
Mysql database indexing tutorial (super detailed)
Go语言 | 01 WSL+VSCode环境搭建避坑指南
Thoroughly understand why network i/o is blocked?
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
2022 the latest big company Android interview real problem analysis, Android development will be able to technology
Shang Silicon Valley Shang preferred project tutorial release
[performance test] jmeter+grafana+influxdb deployment practice
C# 语言的高级应用
随机推荐
How to realize the Online timer and offline timer in the game
C# 语言的高级应用
如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
Get wechat avatar and nickname with uniapp
华律网牵手观测云,上线系统全链路可观测平台
Go语言 | 03 数组、指针、切片用法
IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系
Blue sky drawing bed Apple quick instructions
潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)
#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
HiEngine:可媲美本地的云原生内存数据库引擎
Why can't Bi software do correlation analysis? Take you to analyze
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
2022 the latest big company Android interview real problem analysis, Android development will be able to technology
word如何转换成pdf?word转pdf简单的方法分享!
Shang Silicon Valley Shang preferred project tutorial release
The road of enterprise digital transformation starts from here
Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
After the company went bankrupt, the blackstones came