0%

python3+django安装mysql

  • 这将会在你的当前目录下生成一个 mysite 目录

admin startproject mysite

新建数据库

  • 配置mysite下的setting.py中的数据库设置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test', #数据库名字
    'USER': 'root',
    'PASSWORD': '',
    'HOST': '127.0.0.1',
    'PORT': '3306',
    }
    }
  • 安装py3驱动mysql
    1
    pip install PyMySQL
    找到mysite/mysite/init.py,在里面输入以下内容并保存:
    1
    2
    import pymysql
    pymysql.install_as_MySQLdb()
  • 执行下列命令
    1
    python manage.py runserver
    就会看到如下所示:
    1
    2
    3
    4
    5
    6
    7
    8
    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.
  • 浏览器打开后出现个异常提示:
    1
    2
    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
    1
    2
    DEBUG = False
    ALLOWED_HOSTS = ['*']

最后再次运行:

1
2
python manage.py runserver