PyCharm creates a Django project, Chinese beautification and the process and problem solving of replacing the MySQL database

Recently, I am researching python and django. Since I am a novice, I basically search related tutorials directly on Baidu for configuration and installation. Considering that django has a ui interface, it may be more intuitive, so I mainly create projects based on django. Since the software versions are relatively New, some settings on the Internet always report errors, so I specially record the process of successfully installing django and replacing the MySQL database.

Software environment information:

Computer system: win10 64 bit

PyCharm version: PyCharm 2022.2 (Chinese plug-in installed)

Python version: 3.11

PyMySQL version: 1.0.3

mysqlclient version: 2.1.1

pip version: 23.0.1

1. Preparation work:

1. Go to the python official website to download the latest version 3.11 and install it locally. The process is omitted.

2. To install MySQL Community Server locally , you can directly search the official website of MySQL to download it. During the process, you can set the default database account password. Generally, for local debugging, the account password root is sufficient.

3. Use MySQL Workbench to log in to the default database, and create a new database (the database your django project plans to use), such as: mysite.

4. If you don’t want to use MySQL Community Server, you can also install Xiaopi Panel (phpstudy) locally, and create a database in Xiaopi Panel. The operation is relatively simple. Here I choose Xiaopi Panel.

Note: In the Xiaopi panel, select a version after mysql8.0 to install, and django4.2 does not support lower versions of mysql.

5. You may need to install c++14.0 locally, please download and install it on Baidu.

2. Create a default django project, set Chinese and UI beautification:

6. Open the PyCharm software, select the new project, select Django on the left, and in the more settings, you can preset the app.

7. In the settings.py of the initial project directory, modify the project language (the default is English, here it is replaced with Chinese):

LANGUAGE_CODE = 'zh-hans' #中文语言
TIME_ZONE = 'Asia/Shanghai' #时区

8. Use simpleUI to beautify the background interface: 

pip install django-simpleui

Find INSTALLED_APPS in settings.py and add 'simpleui':

INSTALLED_APPS = [
 'simpleui', #将这个放在第一行
 'django.contrib.admin',

3. Replace with MySQL database:

9. Modify the database connection, find the database part in settings.py, and change it to mysql related:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',#配置数据库类型
        'NAME': 'mysite',#数据库名字
        'USER': 'root',#数据库用户
        'PASSWORD': 'root',#数据库密码
        'HOST' : '127.0.0.1',#数据库地址
        'PORT' : '3306',#数据库端口
    }
}

10. Paste the code in __init__.py (open is empty) in the initial directory:

import pymysql
pymysql.version_info = (2, 1, 1, 'final', 0)  #指定版本,这句才是关键
pymysql.install_as_MySQLdb()

The debugging has been reporting errors before, probably suggesting that mysqlclient requires version 1.4.3 and above , so the version of mysqlclient needs to be specified in the code that introduces pymysql above, and my side is version 2.1.1. In the above code, pymysql.version_info = ( 2, 1, 1, 'final', 0) , use this sentence to specify the version.

Synchronize the database in the terminal:

python manage.py migrate

11. Select the top menu [Tools] > [Run manage.py task], create a background account password in a new window , note that after replacing the mysql database, when entering the password, it will not be displayed in the terminal, and It’s not that the input is not successful , just enter and press Enter to confirm.

# 初始化数据库表

makemigrations

migrate

# 创建admin user

createsuperuser

12. Finally, to test whether it can run, enter python manage.py runserver at the project terminal to start the project:

python manage.py runserver

If the prompt: Starting development server at http://127.0.0.1:8000/, direct access to http://127.0.0.1:8000/ is also normal, then there is no problem, I tested the mysql database created by Xiaopi panel The connection can also be successful, and you can continue to go to the app directory to make models and view templates later.

Guess you like

Origin blog.csdn.net/whoas123/article/details/130018226