美文网首页
python3+django安装mysql

python3+django安装mysql

作者: 望月成三人 | 来源:发表于2019-01-16 19:04 被阅读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
    
    

    相关文章

      网友评论

          本文标题:python3+django安装mysql

          本文链接:https://www.haomeiwen.com/subject/eoiddqtx.html