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中的JSON模块
Apr 08 Python
Python使用functools模块中的partial函数生成偏函数
Jul 02 Python
详解python开发环境搭建
Dec 16 Python
Python3编程实现获取阿里云ECS实例及监控的方法
Aug 18 Python
详解python的ORM中Pony用法
Feb 09 Python
python3+PyQt5实现文档打印功能
Apr 24 Python
Python3.6日志Logging模块简单用法示例
Jun 14 Python
python通过http下载文件的方法详解
Jul 26 Python
python 一篇文章搞懂装饰器所有用法(建议收藏)
Aug 23 Python
python super的使用方法及实例详解
Sep 25 Python
Pytorch 解决自定义子Module .cuda() tensor失败的问题
Jun 23 Python
PyCharm 2020.2下配置Anaconda环境的方法步骤
Sep 23 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
连接到txt文本的超链接,不直接打开而是点击后下载的处理方法
2009/07/01 PHP
深入array multisort排序原理的详解
2013/06/18 PHP
PHP页面实现定时跳转的方法
2014/10/31 PHP
smarty模板引擎之分配数据类型
2015/03/30 PHP
ThinkPHP中数据操作案例分析
2015/09/27 PHP
Session 失效的原因汇总及解决丢失办法
2015/09/30 PHP
javascript web页面刷新的方法收集
2009/07/02 Javascript
JavaScript高级程序设计 阅读笔记(十七) js事件
2012/08/14 Javascript
jQuery基础框架浅入剖析
2012/12/27 Javascript
js检测浏览器版本、核心、是否移动端示例
2014/04/24 Javascript
JavaScript利用fetch实现异步请求的方法实例
2017/07/26 Javascript
微信小程序组件之srcoll-view的详解
2017/10/19 Javascript
vue2.0+koa2+mongodb实现注册登录
2018/04/10 Javascript
JS与CSS3实现图片响应鼠标移动放大效果示例
2018/05/04 Javascript
初试vue-cli使用HBuilderx打包app的坑
2019/07/17 Javascript
JS开发 富文本编辑器TinyMCE详解
2019/07/19 Javascript
JS实现拼图游戏
2021/01/29 Javascript
详解vue父子组件状态同步的最佳方式
2020/09/10 Javascript
[05:05]DOTA2亚洲邀请赛 战队出场仪式
2015/02/07 DOTA
[00:57]深扒TI7聊天轮盘语音出处5
2017/05/11 DOTA
python中的文件打开与关闭操作命令介绍
2018/04/26 Python
Tensorflow实现卷积神经网络的详细代码
2018/05/24 Python
python3使用SMTP发送简单文本邮件
2018/06/19 Python
Python lambda表达式用法实例分析
2018/12/25 Python
Python多线程thread及模块使用实例
2020/04/28 Python
详解pandas映射与数据转换
2021/01/22 Python
HTML5 Web存储方式的localStorage和sessionStorage进行数据本地存储案例应用
2012/12/09 HTML / CSS
HTML中fieldset标签概述及使用方法
2013/02/01 HTML / CSS
HTML5之tabindex属性全面解析
2016/07/07 HTML / CSS
来自全球大都市的高级街头服饰:Pegador
2018/01/03 全球购物
大学生四年生活自我鉴定
2013/11/21 职场文书
应聘医药销售自荐书范文
2014/02/08 职场文书
创业女性典型材料
2014/05/02 职场文书
2015年度护士个人工作总结
2015/04/09 职场文书
辩论赛新闻稿
2015/07/17 职场文书
Golang全局变量加锁的问题解决
2021/05/08 Golang