Table of contents
1.1django is a third-party module, installed with pip install django:
1.2 Python installation directory:
2.1 Steps to create in the terminal
2.2 Use pycharm (Enterprise Edition) to create a django project
2.5 app creation and specification
Note: The loading directory is incorrect, which may cause project errors
2.7 Templates and static files
2.10orm_connecting MySQL module
2.12 configuration of orm_connection to MySQL
2. Django connects to the database
2.14 orm_ create and modify table structure
In the future, I want to adjust the table structure during development
2.15orm- add, delete, modify and check
Notes for studying with Mr. Wu Peiqi at station B.....
1. Install django
1.1django is a third-party module, installed with pip install django :
1.2 Python installation directory:
In addition to putting the installed modules (the source code of the framework, which needs to be imported and used) into ..../Lib/site-pachages, django will also produce a file (this is a tool to help us create projects) Put it in ..../Scripts.
2. Create a project
Projects in django will have some default files and default folders
2.1 Steps to create in the terminal
- open terminal
- Enter a directory (where the project wants to be placed)
D:\django idea\disease predication>
- Execute the command to create the project
在django-admin所在目录执行命令
"D:\software\study software\pycharm\python\Scripts\django-admin.exe" startproject 项目名称
如果D:\software\study software\pycharm\python\Scripts已经加入到系统环境变量,则可执行:
django-admin.exe startproject 项目名称
Implementation process
Execute the command to create the project and an overview of the project file after the creation is successful:
2.2 Use pycharm (Enterprise Edition) to create a django project
Create operation:
2.3 Comparing the two methods
- command line: the created project is standard
- pycharm: Added something by default on the basis of the standard
1. Create a templates directory, and then [delete]
2. There is one more sentence in settings.py [delete]
2.4 Default file introduction
2.5 app creation and specification
App is an application. Large-scale projects can be written in Django. Each large project has many classified small functions, and it supports splitting into small apps.
——项目
——app,用户管理【表结构、函数、HTML模板、CSS】
——app,订单管理
——app,API
——app,网站
每一个app可以有自己独立的数据库的表结构.....
Create app:
Enter the terminal in the directory where the file is located, and execute the python manage.py startapp app01 command to create the app. However, the following error occurred during the creation process: Failed to resolve
Reference: http://t.csdn.cn/imPci
The project has been re-established, and the app can be successfully created at present, and the subsequent operation and learning will start with the newly created djangoProject project
app directory:
2.6 Start and run django
- Make sure the app is registered [settings.py]
- Write the corresponding relationship between URL and view function [urls.py]
- Write view function [views.py]
If the user visits this URL, this function will be executed, and the function returns a "Welcome" string, which will appear on the user's browser.
- Start the django project
- Command line start: python manage.py runserver
- PyCharm starts:
Note: The loading directory is incorrect, which may cause project errors
2.7 Templates and static files
2.7.1 Write another page
After all the configurations are done, the essence of development afterwards is: add a corresponding relationship in urls, and write a view function in views to process the request.
2.7.2 templates
returns an HTML
2.7.3 Static files
- Create a static folder under the app directory
- Import static files
2.8 django's template syntax
The template syntax is developed by django, the essence: write some placeholders in HTML, and replace and process these placeholders by data
Received by the user's browser:
the case
Pseudo Unicom Message Center
http://www.chinaunicom.com/news/list202304.html
Subsequent improvement
2.9 Request and response
3 requests
The GET request is passed on the surface, and the POST request is passed secretly, which cannot be seen on the surface
3 responses
About redirection: the browser sends a request to a certain website, and the return value of a certain website tells the browser that you should go to another place, and the browser goes to this place by itself, which has nothing to do with the website, which is the second form in the figure below:
Case: user login
Error resolution:
The solution to the above error is as follows: By default, Django has one more function than Flask, CSRF token verification
code:
An effect finally achieved:
2.10orm_connecting MySQL module
Django development introduces a mechanism for easier operation of the database. It is easier to operate the database, and an ORM framework is provided internally.
Install third-party modules:
pip install mysqlclient
Error installing:
Solution:
2.11orm-create database
ORM can help us do two things:
- Create, delete, and modify tables in the database (no need to write SQL statements yourself) [Unable to create database]
- Operate the data in the table (no need to write SQL statements) because Django will translate the code you write into SQl statements through ORM
1. Create your own database
- Start the MySQL service
- Self-contained tool to create database
create database gx_day14 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
Basic database learning: (144 messages) Database and identity authentication_Cannon doesn't want to learn Blog-CSDN Blog
2.12 configuration of orm_connection to MySQL
Mysql environment variable configuration of my computer:
Configure environment variables
See if the configuration is successful
2. Django connects to the database
Configure and modify in the settings.py file
The sqllite file database used by the default Database
Change to the following: Django will help us connect to the database by default
2.13orm_class create table
3. Django operation table
- create table
- delete table
- modify table
Create table: in models.py
# UserInfo是类 必须继承 models.Model
class UserInfo(models.Model):
# 字段
name = models.CharField(max_length=32)
password = models.CharField(max_length=64)
age = models.IntegerField()
Excuting an order:
python manage.py makemigrations
python manage.py migrate
Notice:
- Make sure the python interpreter has Mysqlclient installed
- Make sure the current path is the root directory of the current project
- The app needs to be registered in advance (the app is not registered in setting.py, and the tables in models.py will not be submitted to the database)
It is particularly error-prone here, change the interpreter and reinstall
Finally, enter the directory where manage.py is located, open the terminal, and execute the command to successfully install it! ! ! !
However, after the above installation operation, the subsequent table cannot be found in the database
Note: There can be no Chinese in the path of the project, and the location of the project that was later moved, re-executed the following command:
- pip install mysqlcliennt
- python manage.py makemigrations
- python manage.py migrate
Why are there other tables besides app01_userinfo? Because once the command is executed, go back and find the models.py in each app to generate the corresponding table according to the statement, but Django provides many other apps by default (the functions provided by django, these functions are all dependent on some tables of the database) , it can be seen in setting.py, so when the table is generated, some default tables are also generated. If you don’t want to use the corresponding position in the picture below, just comment it out
Enter the command desc app01_userinfo; you can see the created table
2.14 orm_ create and modify table structure
Add (below)/delete tables ( delete the corresponding statement, then re-execute the 2 command statements )
Modify a column in a table
Since there may be data in existing columns, the new column must specify the data corresponding to the new column:
- Enter value manually
- set default
- Allow empty
In the future, I want to adjust the table structure during development
Just operate the class in the models.py file, and then execute the command:
python manage.py makemigrations
python manage.py migrate
2.15orm- add, delete, modify and check
Manipulate data in the table
new data
Write a url to test the function
New data:
# #### 1.新建 ####
# Department.objects.create(title="销售部")
# Department.objects.create(title="IT部")
# Department.objects.create(title="运营部")
# UserInfo.objects.create(name="武沛齐", password="123", age=19)
# UserInfo.objects.create(name="朱虎飞", password="666", age=29)
# UserInfo.objects.create(name="吴阳军", password="666")
delete data:
# #### 2.删除 ####
# UserInfo.objects.filter(id=3).delete()
# Department.objects.all().delete()
# UserInfo.objects.all().delete()
retrieve data:
Get all data matching the criteria
Get a row of data
update data:
# #### 4.更新数据 ####
UserInfo.objects.all().update(password=999)
UserInfo.objects.filter(id=19).update(age=999)
UserInfo.objects.filter(name="朱虎飞").update(age=999)
return HttpResponse("耶耶耶")
Case: User Management
1. Display user list
- url.py
- view.py function
Get information about all users
html rendering
2. Add users
Implement the user input user, and then add to the database
- url.py
- view.py function
GET: see the page, enter content
POST: submit --> write to the database
Add button on the list page, click to jump directly to the add page
3. Delete user
- url.py
- view.py function
需求
http://127.0.0.1:8000/info/delete/?nid=1
http://127.0.0.1:8000/info/delete/?nid=2
http://127.0.0.1:8000/info/delete/?nid=21
def 函数(request):
#.GET在url后边获取传过来的参数,POST是获取请求体传过来的值
nid=request.GET.get("nid")
UserInfo.objects.filter(id=nid).delete()
return HttpResponse("删除成功")