python3.x+django的mysql驱动安装问题

来源:互联网 发布:矩阵中diag是什么意思 编辑:程序博客网 时间:2024/05/22 09:07

Python版本:3.5.1

Django版本:1.9

django使用的数据库改成mysql,在setting.py修改如下配置

修改成:

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        'NAME': 'mydb', #新建数据库名称        'USER': 'python',  #mysql用户名        'PASSWORD': '123456',        'HOST': '127.0.0.1',        'PORT': '3306',  #mysql数据库的端口号    }}

pip中的mysql-python不支持python3.x呗,可能是pip中更新的比较慢吧,那我们就去下载他们的源码安装,然而他的源码最新更新时间是2012年,依然没有对python3.x支持!

这可如何是好?换个支持python3的驱动试试吧,心中万马奔腾,当然是草泥马,于是乎在github上一顿搜索,找到了一个PyMySQL(https://github.com/PyMySQL/PyMySQL ),当然pip中也有它,于是我又飞快的在命令行中打出了:

pip install PyMySQL

非常顺利的就安装成功了,然而Django并不认这个外来的和尚,咋办呢,也好办,找到first/first/__init__.py,在里面输入以下内容并保存:

import pymysqlpymysql.install_as_MySQLdb()

然后我再运行python manage.py runserver时,又爆了一个提示:

You have unapplied migrations; your app may not work properly until they are applied.

Run 'python manage.py migrate' to apply them.

当然这个提示并不影响自带服务器的运行,这时候我们访问http://127.0.0.1:8000,会看到成功提示:

It worked!

Congratulations on your first Django-powered page.

Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [app_label].

You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!

当然了,对于前面那个警告提示,我当然看着不爽,而且他也告诉我怎么做可以解决他,当然要处理啦!我飞快的复制一下内容到命令行中:

python manage.py migrate

然后在重启服务,就很正常啦!

0 0
原创粉丝点击