Hello everyone, I am the blogger of csdn: lqj_ myself
This is my personal blog homepage:
lqj_My blog_CSDN blog-WeChat applet, front-end, python field blogger lqj_I am good at WeChat applet, front-end, python, etc. https://blog.csdn.net/lbcyllqj?spm=1011.2415 .3001.5343 Bilibili welcome attention:Xiao Miao Develop
Xiaomiao Develop's personal space-Xiaomiao Develop personal homepage-哔哩哔哩Video
This article mainly talks about: Quick start, pythonweb development Django framework
Table of contents
1. Manually create the database
cmd command window to create a database
2. Djngo connects to the database
Configure and modify in the setting.py file.
3. Make sure the app is registered
At this point, there may be errors that may occur
The data structure of the data table
Delete table/delete data in table
mysqlclient module
Django supports the mysqlclient module, through which to connect to the mysql database greatly saves some tedious operations that we use in the pymysql module library.
pip install mysqlclient
ORM
ORM can help us do two things:
1. Create, modify, and delete tables in the database (no need to write sql statements) but [the database cannot be created, the database must be created manually in mysql]
2. Manipulate the data in the table (no need to write sql statement)
so:
1. Manually create the database
Start your own mysql service
Method 1: Enter the following command into mysql:
net start 你的mysql名字
Method 2: Enter "Task Manager" > right click to start
cmd command window to create a database
create database 数据库名字;
2. Djngo connects to the database
Configure and modify in the setting.py file.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名字',
'USER': 'mysql用户名',
'PASSWORD': 'mysql密码器',
'HOST': '127.0.0.1', # 安装MySQL数据库的机器
'PORT': 3306 # 端口
}
}
3. Make sure the app is registered
Viewed in setting.py (app01 has been registered)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config'
]
4. Djngo operation table (the naming method of the automatically created table: registered app name + class name "UserInfo")
Write in the models.py file (demonstration code: create a data structure in the app01_userinfo table:
name,password,age)
from django.db import models
class UserInfo(models.Model):
name = models.CharField(max_length=32)
password = models.CharField(max_length=64)
age = models.IntegerField()
Immediately after writing, in the command window in pycharm, enter the command in the root directory of the Django file:
python manage.py makemigrations
At this time, observe whether the py file is automatically added under the migrations folder (as shown in the figure below:)
Then, enter the command:
python manage.py migrate
At this point, there may be errors that may occur
django.db.utils.NotSupportedError: MySQL 5.7 or later is required
This is caused by the incompatibility between the installed Django and mysql versions.
problem solved
1. Lower the version of our Django module library
2. Innovatively install a mysql database above version 8
After solving the compatibility problem, re-run the command: python manage.py migrate
data table in database
The data structure of the data table
Delete table/delete data in table
Comment or delete the class (such as: userinfo class)/data structure (such as: name field) that you want to delete in the models.py file, and re-execute the following command
python manage.py makemigrations
python manage.py migrate
Modify table data structure
When adding a new column to a table, since there may already be data in the existing list, the new column must specify the data corresponding to the new column:
1. Manually enter a value
set default
age = models.IntegerField(default=2)
Allow empty
age = models.IntegerField(null=True,blank=True)
Summarize
In the future, if you want to adjust the table structure during development:
Just operate in the models.py file.
python manage.py makemigrations
python manage.py migrate