Django2.* + Mysql5.7 development environment integration tutorial diagram

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment:

MAC_OS 10.12

Python 3.6

mysql 5.7.25

Django 2.2.3

Prerequisite: Python, Django and MySQL have been successfully installed and can be run separately

1. Database configuration in settings

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    "USER": "user name",
    "PASSWORD": "your password",
    "NAME": "db name"
  }
}

2. Change the default database driver

1. Install pymysql

pip3 install pymysql

2. Replace the driver

Find the main package of the project. For example, if the project is called TEST, find the __init__.py file under the TEST package.

Add the following code:

import pymysqlpymysql.install_as_MySQLdb()

3. Problem solving:

Question 1: mysqlclient version issue

"""
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
"""

Solution: Follow the path in the figure to find the corresponding lines 35 and 36 and comment them out;

Question 2: String encoding and decoding problem

"""
AttributeError: 'str' object has no attribute 'decode'
"""

Solution: Click on the exception tracking information, find line 146 in operations.py and change decode to encode:

Test run:

PS: The reason for modifying the source code is probably the compatibility issue between Django 2, Python 3 and PyMySQL. In addition, the reason for choosing 2.* is because of the official support time.

The default database driver for Django is mysqlclient, but during testing, it was found that it could not run at all, and the import of various mysql libraries failed! .......

Summarize

The above is the illustrated tutorial of Django2.* + Mysql5.7 development environment integration introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Summary of Django connection to MySQL configuration method (recommended)
  • Django uses the existing data table method of Mysql database
  • Django reads Mysql data and displays it on the front end
  • Django restarts after reinstalling MySQL and reports an error: How to solve the problem of No module named 'MySQLdb'
  • Django+mysql configuration and simple operation database example code
  • How to install Python3 and Django under Linux and configure MySQL as the default Django server
  • Deploy Apache+Python+Django+MySQL environment on Linux system
  • Django1.7+python 2.78+pycharm configuration mysql database tutorial
  • Python Django connects to MySQL database to add, delete, modify and query

<<:  In-depth understanding of React Native custom routing management

>>:  Detailed explanation of the use of Linux seq command

Recommend

Practice of multi-layer nested display of element table

There is a requirement for a list containing mult...

MySQL Optimization: Cache Optimization (Continued)

There are caches everywhere inside MySQL. When I ...

Methods of adaptive web design (good access experience on mobile phones)

1. Add the viewport tag to the HTML header. At th...

Solution to MySQL Chinese garbled characters problem

1. The Chinese garbled characters appear in MySQL...

A record of pitfalls in JS regular matching

I recently discovered a pitfall in regular expres...

A brief discussion on this.$store.state.xx.xx in Vue

Table of contents Vue this.$store.state.xx.xx Get...

Detailed explanation of how to manually deploy a remote MySQL database in Linux

1. Install mysql Run the following command to upd...

VUE implements token login verification

This article example shares the specific code of ...

Linux sudo vulnerability could lead to unauthorized privileged access

Exploiting a newly discovered sudo vulnerability ...

Small paging design

Let our users choose whether to move forward or ba...

Detailed analysis of matching rules when Nginx processes requests

When nginx receives a request, it will first matc...

How to implement insert if none and update if yes in MySql

summary In some scenarios, there may be such a re...

A brief discussion on Nginx10m+ high concurrency kernel optimization

What is high concurrency? The default Linux kerne...

JS version of the picture magnifying glass effect

This article shares the specific code of JS to ac...

7 native JS error types you should know

Table of contents Overview 1. RangeError 2. Refer...