当前位置:网站首页> Django使用mysqlclient服务连接并写入数据库的操作过程
Django使用mysqlclient服务连接并写入数据库的操作过程
2022-07-05 18:58:00 【1024问】
Django使用mysqlclient服务连接并写入数据库
准备
一、安装mysqlclient服务
二、settings.py文件
1.将子应用写入
2.配置数据库
三.配置urls.py文件
1.初始化应用下的urls.py文件
2.子应用下的urls.py文件
四.配置models.py模型类
五.配置views.py视图类文件
六.在template中编辑HTML页面[比较简易]
七.创建并执行可迁移文件[做过的请省略这一步]
八.效果
九.小结
Django使用mysqlclient服务连接并写入数据库准备1.创建Django程序,安装Django服务(详情请看上上节)
2.创建子应用(详情请看上上节)
3.连接数据库服务,创建一个新的数据库,使用旧的数据也可以.
在Terminal控制台输入:
二、settings.py文件1.将子应用写入pip install mysqlclient==2.0
后面是版本号,可写可不写
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'stu'# 创建的子应用名称]
2.配置数据库DATABASES = { 'default': { # 连接本地mysql数据库 'ENGINE': 'django.db.backends.mysql', 'NAME': 'student',# 数据库名 'USER': 'root',# 用户名 'PASSWORD': 'root',# 密码 'HOST': 'localhost',# 本地ip 'PORT': 3306,# 端口号 }}
三.配置urls.py文件1.初始化应用下的urls.py文件from django.contrib import adminfrom django.urls import path, includeurlpatterns = [ path('admin/', admin.site.urls), path('stu/', include('stu.urls'))# 接入子应用]
2.子应用下的urls.py文件from django.urls import pathfrom stu import viewsurlpatterns = [ path('gosigt/', views.gosigt),# 路径 path('insigt/', views.insigt),# 路径]
四.配置models.py模型类from django.db import modelsclass Stu(models.Model):# 表内的列名: sname=models.CharField(max_length=10) spwd=models.CharField(max_length=10) # 给字表编辑名称 class Meta: db_table='stu'
五.配置views.py视图类文件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')# 获取表单内容 spwd=request.POST.get('spwd') if(sname!="" and spwd!=""): # .save()将数据写入数据库 Stu(sname=sname, spwd=spwd).save() return HttpResponse('<h1>注册成功!</h1>') return HttpResponse('<h1>注册失败!</h1>')
六.在template中编辑HTML页面[比较简易]<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>注册</title></head><body> <form method="post" action="/stu/insigt/"> /*post方法需要写下面这一段代码*/ {% csrf_token %} 用户名:<input type="text" name="sname"><br/> 密 码:<input type="password" name="spwd"><br/> <input type="submit" value="注册"> </form></body></html>
七.创建并执行可迁移文件[做过的请省略这一步]在Terminal控制台输入:
八.效果创建迁移文件:python manage.py makemigrations
执行迁移文件:python manage.py migrate stu
执行迁移文件后面的stu是我子应用的名称
本章概述了在Django框架中使用mysqlclient服务实现连接并写入数据库的操作,内容都是干货,需要熟悉使用.
有哪里不足或者有更好的建议,欢迎留言吐槽,有哪里不懂的小伙伴,可以私信我,我会一一答复,感谢认可,感谢支持!
到此这篇关于Django使用mysqlclient服务连接并写入数据库的文章就介绍到这了,更多相关Django mysqlclient服务连接内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Cf:b. almost Terry matrix [symmetry + finding rules + structure + I am structural garbage]
- 100million single men and women supported an IPO with a valuation of 13billion
- 输油管的布置数学建模matlab,输油管布置的数学模型
- 2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
- 2022 latest intermediate and advanced Android interview questions, [principle + practice + Video + source code]
- 2022 latest Android interview written examination, an Android programmer's interview experience
- EMQX 5.0 正式发布:单集群支持 1 亿 MQTT 连接
- 2022年5月腾讯云开发者社区视频月度榜单公布
- Debezium系列之:解析默认值字符集
- c语言简便实现链表增删改查「建议收藏」
猜你喜欢
华为让出的高端市场,小米12S靠徕卡能抢到吗?
决策树与随机森林
从外卖点单浅谈伪需求
Mariadb root用户及普通用户的密码 重置
CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]
尚硅谷尚优选项目教程发布
一朵云开启智慧交通新未来
彻底理解为什么网络 I/O 会被阻塞?
5年经验Android程序员面试27天,2022程序员进阶宝典
Isprs2022/ cloud detection: cloud detection with boundary nets
随机推荐
ROS installation error sudo: rosdep: command not found
JAD的安装、配置及集成IDEA
Startup and shutdown of CDB instances
为什么 BI 软件都搞不定关联分析?带你分析分析
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
2022最新大厂Android面试真题解析,Android开发必会技术
Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
Get wechat avatar and nickname with uniapp
机器学习基础(三)——KNN/朴素贝叶斯/交叉验证/网格搜索
Blue sky drawing bed Apple quick instructions
2022年5月腾讯云开发者社区视频月度榜单公布
JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
vagrant2.2.6支持virtualbox6.1版本
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
Windows Oracle open remote connection Windows Server Oracle open remote connection
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
块编辑器如何选择?印象笔记 Verse、Notion、FlowUs
彻底理解为什么网络 I/O 会被阻塞?
Golang through pointer for Range implements the change of the value of the element in the slice