当前位置:网站首页>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 . effect Nine . SummaryCreate 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 !
边栏推荐
- 出海十年:新旧接力,黑马崛起
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
- #夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
- China law network joins hands to observe the cloud, and the online system is a full link observable platform
- Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
- 2022最新大厂Android面试真题解析,Android开发必会技术
- XML basic knowledge concept
- Go语言学习教程(十六)
- Oracle Chinese sorting Oracle Chinese field sorting
- PHP利用ueditor实现上传图片添加水印
猜你喜欢
Can Leica capture the high-end market offered by Huawei for Xiaomi 12s?
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
Go deep into the underlying C source code and explain the core design principles of redis
RedHat7.4配置yum软件仓库(RHEL7.4)
【AI 框架基础技术】自动求导机制 (Autograd)
Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
2022 the latest big company Android interview real problem analysis, Android development will be able to technology
Applet modification style (placeholder, checkbox style)
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
MySQL数据库索引教程(超详细)
随机推荐
Password reset of MariaDB root user and ordinary user
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
Fuzor 2020軟件安裝包下載及安裝教程
R语言可视化散点图(scatter plot)图、为图中的部分数据点添加标签、始终显示所有标签,即使它们有太多重叠、ggrepel包来帮忙
Use file and directory properties and properties
Decision tree and random forest
Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
Startup and shutdown of CDB instances
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Android面试,android音视频开发
Advanced application of C # language
Applet modification style (placeholder, checkbox style)
The monthly list of Tencent cloud developer community videos was released in May 2022
Ten years at sea: old and new relay, dark horse rising
关于 Notion-Like 工具的反思和畅想
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]
Windows Oracle open remote connection Windows Server Oracle open remote connection
Pandora IOT development board learning (HAL Library) - Experiment 8 timer interrupt experiment (learning notes)
Benefits of automated testing