总所周知,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
- you can fork thepymysql program on github(https://github.com/PyMySQL/PyMySQL/)
- 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).