How to create a Django project + connect to MySQL

How to create a Django project + connect to MySQL

1: django-admin.py startproject project name

2: cd project name

3: Modify setting.py

(1): ALLOWED_HOSTS = [] => ALLOWED_HOSTS = [“*”]

(2): LANGUAGE_CODE = 'en-us' => LANGUAGE_CODE = 'zh-hans'

(3): TIME_ZONE = 'UTC' => TIME_ZONE = 'Asia/Shanghai'

4: Create the app

django-admin.py startproject app name

5: Modify setting.py

INSTALLED_APPS array added => 'app name'

6: Add a utils folder under the project directory to encapsulate multiple app public methods

7: Add a new util file in the app directory to encapsulate common methods of the app

8: Add a new view folder under the app directory to store the logic code of each page

9: Add a new routing file (urls.py) in the app directory to store all routes under this app

Format:
	from django.urls import path
	from .views import wx_pay
	urlpatterns = [
		 path("test", wx_pay.wx_pay().test, name="test"), # test ]

10: Add app routing to project routing; modify urls.py in the project directory

from django.contrib import admin
from django.urls import path, include
from H5 import urls as h5_urls
urlpatterns = [
		path('admin/', admin.site.urls),
	path("h5/", include(h5_urls))
]

At this point, the Django project directory structure has been configured , and you can then enter the development phase.

We often use databases during development; here's how to configure the database

11: Modify setting.py in the project directory

default:
DATABASES = {
 'default': {
  'ENGINE': 'django.db.backends.sqlite3',
  'NAME': BASE_DIR / 'db.sqlite3',
 }
}
After modification:
DATABASES = {
 'default': {
 'ENGINE' : 'django.db.backends.mysql',
 'NAME': 'Database name (hereinafter referred to as DATABASE1)',
 'USER':'Username',
 'PASSWORD': 'Database password',
 'HOST':'ip address',
 'PORT':'Port'
 }
}

12: Open the database and add the database name configured above (DATABASE1)

13: Configure the model and enter the models.py file in the app directory

import time

from django.db import models

# Create your models here.

 class Test(models.Model):
  str = models.CharField("string", max_length=30, null=True, blank=True)
  num = models.IntegerField("number", default=1, null=True, blank=True)
  create_time = models.DateTimeField("time", default=time.strftime('%Y-%m-%d %H:%M:%S'), blank=True)

14: Migrate data to the database

python manage.py makemigrations

python manage.py migrate

15: At this time, the following error is likely to be reported:

Traceback (most recent call last):
Omit some in the middle
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/MySQLdb/__init__.py", line 24, in <module>
version_info, _mysql.version_info, _mysql.__file__

NameError: name '_mysql' is not defined

This error is mainly because Mysqldb is not compatible with python3.5 and later versions

16: Modify __init_.py in the project directory and add the following code

import pymysql

pymysql.version_info = (1, 4, 13, "final", 0)

pymysql.install_as_MySQLdb()

Well, now all the commonly used configurations are complete.

This is the end of this article about how to create a Django project + connect to MySQL. For more information about how to create a Django project and connect to MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief understanding of the Django project application creation process
  • Django project creation and management implementation process detailed explanation
  • Django framework installation and project creation process analysis
  • Several ways to create Django projects in Python3 (3 kinds)
  • Detailed explanation of creating Python Django projects and applications

<<:  Implementation of 2D and 3D transformation in CSS3

>>:  Perfect solution to the problem that Navicat cannot connect after installing mysql in docker

Recommend

MySQL index cardinality concept and usage examples

This article uses examples to explain the concept...

Tutorial on installing and configuring MySql5.7 in Alibaba Cloud ECS centos6.8

The default MySQL version under the Alibaba Cloud...

Vue implements login jump

This article example shares the specific code of ...

Detailed explanation of how to use the mysql backup script mysqldump

This article shares the MySQL backup script for y...

How to configure Bash environment variables in Linux

Shell is a program written in C language, which i...

Steps to install MySQL 5.7.10 on Windows server 2008 r2

Install using the MSI installation package Downlo...

Install MySQL 5.7.17 in win10 system

Operating system win10 MySQL is the 64-bit zip de...

When you enter a URL, what exactly happens in the background?

As a software developer, you must have a complete...

Public free STUN servers

Public free STUN servers When the SIP terminal us...

Detailed installation tutorial of mysql 5.7 under CentOS 6 and 7

You always need data for development. As a server...

Float and Clear Float in Overview Page

1. Float: The main purpose is to achieve the effe...

WeChat Mini Program QR Code Generation Tool weapp-qrcode Detailed Explanation

WeChat Mini Program - QR Code Generator Download:...

Briefly describe the difference between Redis and MySQL

We know that MySQL is a persistent storage, store...

How to start a Java program in docker

Create a simple Spring boot web project Use the i...