Django mysqlclient安装和使用详解


Posted in Python onSeptember 17, 2020

一、安装mysqlclient

网上看到很过通过命令:pip install mysqlclient 进行安装的教程,但是我却始终安装失败,遇到的错误千奇百怪,后来通过自己下载mysqlclient客户端终于安装成功;

首先打开网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/并找到下面图中的内容部分:

Django mysqlclient安装和使用详解

根据自己的需要,我选择的是最下边的cp38(目测cp38应该是C++版本,下载下来的文件通过pip install 进行安装的时候会进行c++编译,如果你的电脑(我是Windows)上没有安装VC++,那么找个新版本的安装一下即可:https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads)记住如果没有C++,就先安装C++这个;

下载好mysqlclientt之后如下(只要下载1个,我系统是64位,所以先下载的64位的,结果用不了,所以又下载了32位的才成功,所以建议先下载32位的试试):

Django mysqlclient安装和使用详解

打开控制台(开始->运行->cmd):

第一步:cd 到下载的mysqlclient文件所在的目录:cdC:\Users\Yeat\Downloads\mysqlclient

第二步:执行安装命令:pip installmysqlclient-1.4.4-cp38-cp38-win32.whl

如果成功的话会看到:

C:\Users\Yeat\Downloads>pip install mysqlclient-1.4.4-cp38-cp38-win32.whl
Processing c:\users\yeat\downloads\mysqlclient-1.4.4-cp38-cp38-win32.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.4

C:\Users\Yeat\Downloads>当然如果失败的话,那很可能看到类似下图的画面:

C:\Users\Yeat>pip install mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl
WARNING: Requirement 'mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl' looks like a filename, but the file does not exist
ERROR: mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl is not a valid wheel filename.

C:\Users\Yeat>pip install MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl
WARNING: Requirement 'MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl' looks like a filename, but the file does not exist
ERROR: MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl is not a valid wheel filename.

C:\Users\Yeat>pip install MySQL_python‑1.2.5‑cp27‑none‑win_amd64
ERROR: Invalid requirement: 'MySQL_python‑1.2.5‑cp27‑none‑win_amd64'

C:\Users\Yeat>cd C:\Users\Yeat\Downloads

C:\Users\Yeat\Downloads>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
ERROR: MySQL_python-1.2.5-cp27-none-win_amd64.whl is not a supported wheel on this platform.

C:\Users\Yeat\Downloads>pip install mysqlclient-1.4.4-cp38-cp38-win_amd64.whl
ERROR: mysqlclient-1.4.4-cp38-cp38-win_amd64.whl is not a supported wheel on this platform.

失败,那就换下载的mysqlclient版本,只能提供这个办法了!!!!

二、在Django框架里使用mysql

1.进入项目工程目录执行命令:django-admin startapp TcesApp,我的完整命令是:C:\Users\Yeat\PycharmProjects\untitled>django-admin startapp TcesApp,前面的部分是我的工程目录路径;

2.命令执行完毕后工程里会增加TcesApp目录如图:

Django mysqlclient安装和使用详解

3.进入models.py中创建与你的数据库表相对应的对象model,我的内容如下:

from django.db import models

class e_exams(models.Model):
 ID = models.CharField(max_length=50),
 ExamName = models.CharField(max_length=50)
 ExamCode = models.CharField(max_length=50)
 SceneID = models.CharField(max_length=50)
 Creater = models.CharField(max_length=50)
 CreateTime = models.DateTimeField()
 State = models.CharField(max_length=50)
 Field_Char1 = models.CharField(max_length=50)
 Field_Char2 = models.CharField(max_length=50)
 Field_Char3 = models.CharField(max_length=50)

 class Meta:
  db_table = 'e_exams' #数据表名称

我的表结构 e_exams:

Django mysqlclient安装和使用详解

在models.py中可以创建过个表的model。

4.在admin.py中注册model:

from django.contrib import admin
from . import models

# Register your models here.
admin.site.register(models.e_exams)

5.在setting.py中添加app名称(上边的名称 django-admin startapp TcesApp 的名称):

Django mysqlclient安装和使用详解

6.还是在settings.py中修改DATABASES内容如下:

Django mysqlclient安装和使用详解

完整配置:

DATABASES = {
 'default': {
  'ENGINE': 'django.db.backends.mysql',
  'NAME': 'tces',
  'USER': 'root',
  'PASSWORD': 'Unity3du#d112233',
  'HOST': 'nas.yeatsoft.com',
  'PORT': '3306',
  'OPTIONS': {
   "init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
  }
 }
}

其中NAME是你的数据库名称,HOST是数据库地址,其它的大家都知道。

7.接下来我们到views.py(或者自己创建的py文件)中编写代码主要看 addExam 这个方法:

from django.http import HttpResponse
from django.shortcuts import render
from TcesApp.models import e_exams

def hello(request):
 return HttpResponse('home page!')


def helloworld(request):
 context = {}
 context['value'] = 'hello world!'
 return render(request, 'helloworld.html', context)

def addExam(request):
 exam = e_exams()
 exam.ID = '100001'
 exam.SceneID = '1001',
 exam.ExamName = '期末考试'
 exam.save()
 context = {}
 context['value'] = exam.ExamName + '数据添加成功!'
 return render(request,'helloworld.html',context)

其中helloworld.html是放在templates中的前端页面:

Django mysqlclient安装和使用详解

context['value']就是html页面中的{{value}}

8.到urls.py中添加路径完整代码如下:

from django.contrib import admin
from django.urls import path
from . import home

urlpatterns = [
 path('admin/', admin.site.urls),
 path('home/', home.hello),
 path('helloworld/', home.helloworld),
 path('add/',home.addExam)
]

三、运行效果如下:

Django mysqlclient安装和使用详解

Django mysqlclient安装和使用详解

Django mysqlclient安装和使用详解

 到此这篇关于Django mysqlclient安装和使用详解的文章就介绍到这了,更多相关Django mysqlclient安装使用内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
理解python正则表达式
Jan 15 Python
使用Python的PIL模块来进行图片对比
Feb 18 Python
Python日期时间对象转换为字符串的实例
Jun 22 Python
Python 删除整个文本中的空格,并实现按行显示
Jul 24 Python
python使用MQTT给硬件传输图片的实现方法
May 05 Python
pytorch实现保证每次运行使用的随机数都相同
Feb 20 Python
tensorflow中tf.reduce_mean函数的使用
Apr 19 Python
python 双循环遍历list 变量判断代码
May 04 Python
在 Python 中使用 7zip 备份文件的操作
Dec 11 Python
python中子类与父类的关系基础知识点
Feb 02 Python
Python基于爬虫实现全网搜索并下载音乐
Feb 14 Python
python上下文管理的使用场景实例讲解
Mar 03 Python
Pycharm2020最新激活码|永久激活(附最新激活码和插件的详细教程)
Sep 29 #Python
Django返回HTML文件的实现方法
Sep 17 #Python
Pycharm新手使用教程(图文详解)
Sep 17 #Python
Django修改app名称和数据表迁移方案实现
Sep 17 #Python
Python request中文乱码问题解决方案
Sep 17 #Python
python如何使用腾讯云发送短信
Sep 17 #Python
通俗易懂了解Python装饰器原理
Sep 17 #Python
You might like
测试您的 PHP 水平的题目
2007/05/30 PHP
PHP 5.3 下载时 VC9、VC6、Thread Safe、Non Thread Safe的区别分析
2011/03/28 PHP
gd库图片下载类实现下载网页所有图片的php代码
2012/08/20 PHP
解决PHP4.0 和 PHP5.0类构造函数的兼容问题
2013/08/01 PHP
phpStudy2016 配置多个域名期间遇到的问题小结
2017/10/19 PHP
浅析JQuery UI Dialog的样式设置问题
2013/12/18 Javascript
js中window.open打开一个新的页面
2014/08/10 Javascript
总结Javascript中的隐式类型转换
2016/08/24 Javascript
JavaScript cookie详解及简单实例应用
2016/12/31 Javascript
jQuery实现手机上输入后隐藏键盘功能
2017/01/04 Javascript
实现图片首尾平滑轮播(JS原生方法—节流)
2017/10/17 Javascript
JQueryDOM之样式操作
2019/03/27 jQuery
多页vue应用的单页面打包方法(内含打包模式的应用)
2020/06/11 Javascript
vue 判断页面是首次进入还是再次刷新的实例
2020/11/05 Javascript
[01:04:48]VGJ.S vs TNC Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
Python中的闭包详细介绍和实例
2014/11/21 Python
Python制作爬虫采集小说
2015/10/25 Python
简单谈谈Python中的闭包
2016/11/30 Python
python获取多线程及子线程的返回值
2017/11/15 Python
Python元字符的用法实例解析
2018/01/17 Python
详解python的sorted函数对字典按key排序和按value排序
2018/08/10 Python
对python 操作solr索引数据的实例详解
2018/12/07 Python
Django REST framework视图的用法
2019/01/16 Python
python实现微信防撤回神器
2019/04/29 Python
pytorch Dataset,DataLoader产生自定义的训练数据案例
2021/03/03 Python
Three Graces London官网:英国奢侈品牌
2021/03/18 全球购物
PHP两种查询函数array/row的区别
2013/06/03 面试题
求职自荐信
2013/12/14 职场文书
领导证婚人证婚词
2014/01/13 职场文书
机关作风建设自查报告
2014/10/22 职场文书
2015年党小组工作总结
2015/05/26 职场文书
运动会通讯稿600字
2015/07/20 职场文书
Nginx 过滤静态资源文件的访问日志的实现
2021/03/31 Servers
Vue实现下拉加载更多
2021/05/09 Vue.js
教你如何用Python实现人脸识别(含源代码)
2021/06/23 Python
忘记Grafana不要紧2种Grafana重置admin密码方法详细步骤
2022/04/07 Servers