Django:mysql数据库链接操作

254 阅读2分钟

第四篇

讲讲Django链接mysql叭

1.安装Django和mysql

首先呢,你得安装mysql,然后在安装Django库,小编用的是windows环境下python3.6,虚拟环境安装的Django==2.2.27
然后就是创建项目:Django-admin startproject myproject

2.修改设置

进入项目目录,有一个manage.py和myproject目录,再进入这个myproject目录,编辑settings.py,找到如下所在位置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
    }
}

进行修改:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',    # 数据库引擎
        'NAME': 'mydatabase',                    # 数据库名
        'USER': 'mydatabaseuser',                # 用户名
        'PASSWORD': 'mypassword',                # 密码
        'HOST': '127.0.0.1',                     # 数据库地址,我是本机IP
        'PORT': '3302',                          # 端口号
    }
}

!!! 记得在mysql创建好数据库。
此时运行python manage.py runserver会出错:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

别急,是因为还没完
接着编辑同目录下的__init__.py(里面空空如也~),加上:

import pymysql
pymysql.install_as_MySQLdb()

似乎好啦~ 但是,又出现一个问题:

  File "D:\Git_repos\django-blog\Venv\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query
    query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'

小场面,根据这提示的File路径,找到operations.py,第146行,把query = query.decode(errors='replace')修改为query = query.encode(errors='replace'),至于它的问题原理,我母鸡呀,也不想知道了。

3.迁移数据

现在出现这个:

You have 17 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.

翻译 : 您有 17 个未应用的迁移。在应用应用迁移之前,您的项目可能无法正常工作:管理员、身份验证、内容类型、会话。 运行"python manage.py migrate"来应用它们。
这会在数据库里创建出INSTALLED_APPS中的应用所需的数据表,do it!

PS D:\Git_repos\django-blog\myproject> python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
                                                            Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

噼里啪啦出现一堆OK,然后你数据库多了辣么多东西,说明可以了!\

附:Django提供了一个轻量级的Web服务程序,无需在生产环境即可快速测试开发中的站点。启动这个服务之后,会检查所有的代码是否正确,还可以在代码被修改之后,自动重新载入修改后的代码,但部分情况下比如向项目中加入了新的文件,还需要手工关闭服务再重新启动。\

提供一手Django2.2文档:docs.djangoproject.com/zh-hans/2.2…

(又是懒懒地更新记录文章ing)(又是懒懒地更新记录文章ing)