128 Django catalog explanation and MTV architecture, registration function realization

Yesterday review

1 django目录介绍
	-urls 路由和视图函数的映射关系
	-models 视图函数
	-models 数据库相关操作
	-settings 配置文件
2 app是什么?
	-项目中独立的功能,通常做成一个app
	-python3 manage.py startapp app01
3 django项目的启动方式
	-命令行:python manage.py runserver 127.0.0.1:8080
	-pycharm
4 配置文件
	-是否调试模式:开发阶段都是true,上线都是false
	-注册app
	-中间件
	-数据库链接配置
	-静态文件配置
5 静态文件
	-1 先创建目录(static)
	-2 在settings.py中配置:
	STATIC_URL = '/static/'
	# 再配一个STATICFILES_DIRS列表(列表内写路径)
	STATICFILES_DIRS = [
		os.path.join(BASE_DIR, 'static')
	]
	-3 在 static文件夹中创建目录:
		-js
		-css
		-img
		-其他第三方模块
	-4 模版文件中使用 (本质原理)
	  -/static/js/jquery.min.js
6 路由配置
	-url(r'^index', views.index),
7 视图
	-请求对象
		request.method---(get/post)
		request.GET---(当作字典)
		request.POST---(当作字典)
		ps:post请求也可以在地址栏携带数据
	-响应对象
		HttpResponse  返回字符串
		render(request,'模版名字')  返回模版
		redirect('/index或者http://www.baidu.com') 返回重定向
8 登陆案例
	

Today

1 Login function, link to mysql

2 MTV和MVC

1 django是MTV架构,本质也是MVC
	-M:model,数据库相关操作
	-T:template,模版文件(就是MVC中的V)
	-V:view,视图(路由+V=MVC中的C)
2 MVC架构:主流的web框架都是MVC
	-web应用分为模型(M),视图(V)和控制器(C)
	-M:model,数据库相关操作
	-C:controler 控制器,逻辑相关,逻辑代码
	-V:view 视图
3 详情:
https://www.cnblogs.com/liuqingzheng/p/13725794.html

3 Django request life cycle

0 近几年python中的几个web框架,sanic,fastapi(异步框架)

1 python中的web服务器都符合wsgi协议

2 web服务器,任何语言都会有一个web服务器,负责把http请求转成这门语言的变量
	-python:wsgiref(性能很低),uwsgi(c语言写的,200多并发量)
	-java:tomcat,jboss(300多并发量)
	-php:php服务器
	
3 请求生命周期图解:

image-20200924175613811

4 Virtual environment explanation, pycharm configuration

1 虚拟环境作业:隔离环境,每个项目有自己依赖的模块和包,不同模块和包的版本	就不会相互影响
2 a路径、b基于哪个解释器、c是否继承基础解释器的模块、d是否可以被其他项目	 使用

5 orm overview

1 orm:对象关系映射(跟语言无关)
	数据库中的表-------->对应程序的一个类
	数据库中的一行数据--->对应程序中的一个对象

2 python中常见orm框架
	-django的orm框架
	-sqlachemy orm框架
	
3 java,java中写web的项目:
	-ssh框架:spring+structs(有漏洞)+hibernate(orm框架)
	-ssm框架:spring+springMVC+mybatis(orm框架,可以写原生sql)
	
	-sb框架:springboot,内置tomcat(现在主流)
	-springcloud:微服务(现在主流)
	
4 orm能干的事
	-创建表(不能创建数据库,手动创建数据库)
	-增加删除表内字段
	-增删查改数据

The use of orm in 7django

0 sqlite:也是个数据库,文件数据库,一个库就是一个文件,不需要单独安装
  -现在就在用,也要用到关系数据库,不需要连数据库,
  -移动开发本地存储数据,存在sqlite中

1 创建个UserInfo表,在model中写一个类
2 表中有字段(类属性),字段有属性,

第一步在models中要写类:
  class UserInfo(models.Model):
	# 字段属性-->后面那个对象决定的
	# id字段自增,并且是主键
	id = models.AutoField(primary_key=True)
	# 名字字段是varchar类型,长度为32(唯一约束,是否是索引,默认值是,是否可为空)
	name = models.CharField(max_length=32)
	# 密码字段
	password = models.CharField(max_length=64)
第二步,把表创建出来(执行两个命令):
  python manage.py makemigrations
  python manage.py migrate
	

8 User registration, show case

1 Write a UserInfo class in models.py, be sure to inherit models.Models, and then use the command to create the table.
2 Create register.html under tempates, and submit the form in the form required by UserInfo.
3 In urls.py Add routing in: url(^register,"views.register"), url(^userlist,"views.user_list")
4 Write the register function in views.py, instantiate a user object with the data obtained in the page, and return redirect('/userlist'), jump to the view user list page and
write the user_list function, use models.UserInfo.object.all() to query all user data, and replace the content in the template file, and return render(request,'userlist. html', context=('userlist': userlist))
5 Create userlist.html under tempates, traverse the data in userlist and display it.

operation

1 Organize

2 Register, display user list

3 After registration, jump to the login page (pymysql or orm)

4 Jump to view user list after login

supplement:

1 The nature of static files

STATIC_URL = '/static/'

URL中的/static/是模版文件中的src地址,也可以改成其他.

而且之前在本地中的地址都是'../static/css/my.css'
而在模版文件中的地址则是'/static/css/my.css'
这里面的的static是来自于STATIC_URL里的'/static/',浏览器请求模版文件之后再请求静态文件(2次请求),这个请求不是在模版文件所在的路径向上去找,而是直接通过STATIC_URL去请求的.
------------------------------------------------------------
STATICFILES_DIRS = [
   os.path.join(BASE_DIR, 'static')
]
这里的是自己创建的本地静态文件的文件夹路径,可以随意命名,但最好叫static文件夹.

As shown:
Insert picture description here
Insert picture description here

2 What is the longest varchar type

varchar(2555) utf8 = 2555*2 not super long
varchar(2555) utf8mb4 = 2555 *4 super long

3 Create and use a virtual environment under the command line

Guess you like

Origin blog.csdn.net/qq_40808228/article/details/108855512
Recommended