python3.x + django1.9.4 +mysql

  总所周知,python3已经没有了我们熟知的 mysqldb module,转而拥有了pymysql module,那么我们怎么通过pymysql来使django和mysql结合呢?以下content将会给出解答,请耐心浏览。


Step one : install your mysql

find the answer on the net by yourself

Step two : enter mysql,create a database for your django project

with follow command example:(shell pattern)

CREATE DATABASE {name} DEFAULT CHARSET=utf8;

Step three : install pymysql

  1. you can fork thepymysql program on github(https://github.com/PyMySQL/PyMySQL/)
  2. move in the folder you clone,and install it:
python setup.py install

Step four : import pymysql module for your django program

assuming the django program named ‘mypro’,you should append follow sentences:

import pymysql
pymysql.install_as_MySQLdb()

to init.py in mypro/mypro

Step five : deploy mysql information

as the folder above,you deploy your mysql information in setting.py where there is ‘DATABASES’ variable:

DATABASES = { 
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'the name of your database',
        'USER': 'your username for mysql',
        'PASSWORD': 'your password for mysql',
        'HOST': 'host of mysql',  # generally 127.0.0.1
        'PORT': 'port of mysql',  # generally 3306
    }   
}

Step six : synchronize the database

commands are here:

python manage.py makemigrations
python manage.py migrate

if it is successful, there will be a common output(not be too long).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值