python3+django安装mysql
2019-01-16 本文已影响14人
望月成三人
创建一个项目
- 用cd进入到保存项目的文件夹下
- 执行命令
django-admin startproject mysite
这将会在你的当前目录下生成一个 mysite 目录
新建数据库
- 配置mysite下的setting.py中的数据库设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test', #数据库名字
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
- 安装py3驱动mysql
pip install PyMySQL
找到mysite/mysite/init.py,在里面输入以下内容并保存:
import pymysql
pymysql.install_as_MySQLdb()
- 执行下列命令
python manage.py runserver
就会看到如下所示:
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
February 06, 2017 - 14:47:11
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
- 浏览器打开后出现个异常提示:
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!
- 设置setting.py
DEBUG = False
ALLOWED_HOSTS = ['*']
最后再次运行:
python manage.py runserver