python的Web框架,类视图
类视图
范式
from django.views import View # 继承View
class IndexView(View):
def get(self, request):
#写法和函数视图一样
pass
在urls中调用的时候,和函数视图配置是不一样的。
# as_view() 类视图是需要运行
path('index/',views.IndexView.as_view(), name='index')
通用视图
简单应用
view中的定义
from django.views.generic import ListView class StudentListView(ListView):
# 指定使用的页面
template_name = 'teacher/student_list.html' # 获取模型数据
model = Students
html 中应用
# 此处的students_list,为view中定义的name,如果不定义则为object_list
{% for student in students_list %}
<p>{{ student }}</p> {% endfor %}
模块使用
class StudentListView(ListView):
# 指定使用的页面
template_name = 'teacher/student_list.html' # 获取模型数据
model = Students # 对html中的获取的名字进行修改
context_object_name = 'students_list' # 每页显示的数据
paginate_by = 3 # 页面名称
section = '学生列表' # 搜索字段
# search = self.request.GET.get('search','').strip() # 通过这个方法,改变传递在前面的模板对象列表。
def get_queryset(self):
search = self.request.GET.get('search','').strip()
per_page = self.request.GET.get('per_page', 5)
self.paginate_by = int(per_page) if search:
if search.isdigit():
sts = self.model.objects.filter(Q(qq=search)|Q(phone=search),is_deleted=False).order_by('-e_time')
else:
sts = self.model.objects.filter(name_contains=search,is_deleted=False).order_by('-e_time')
else:
ses = self.models.objects.filter(is_delete=False).order_by('-e_time')
return sts # 上下文管理,context内容传给html
def get_context_data(self, **kwargs):
context = super().get_context(**kwargs) # 继承父类
context['section'] = self.section
context['search'] = self.request.GET.get('search','').strip() # 获取的搜索内容
context['page'] = int(self.request.GET.get('page', 1)) # 获取的当前页码
context['per_page'] = self.paginate_by #继承的ListView自动帮我们做了分页,会根据self.paginate_by,page,per_page的参数来进行分页,和之前写的当前页的数据(sts=paginator.get_page(page))是一个意思。
context['students'] = context['page_obj']
return context
pege_obj html中的使用
{{ page_obj }} 在网页端显示的方式是一个对象:
<Page 1 of 6>
DetailView
from django.views.generic import DetailView class StudentDetailView(DetailView):
template_name = 'teacher/detail'
model = Students
# urls.py中 应用 path('detail/<int:pk>',view.StudentDetailView.as_view(),name='detail')
html 中应用
{{ object.name }}
{{ object.sex }}
{{ object.studentsdetail.college }}
类视图的权限装饰
在urls.py文件中使用
from django.contrib.auth.decorators import login_required path('student/',login_required(views.StudentListView.as_view()),name='stuent')
# 一般使用在项目根urls中
view中使用的权限类装饰
方法一:定义函数 from django.utils.decorators import method_decorator class StudentListView(ListView):
pass
当前的通用类视图没有显式指示一个get、post # 分发get,post
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(*args, **kwargs) def get_queryset(self): def get_context_data(self.**kwargs):
方法二:装饰类 @method)decorator(login_required, name='dispatch')
class StudentListView(ListView):
pass
python的Web框架,类视图的更多相关文章
- Python之Web框架Django
Python之Web框架: Django 一. Django Django是一个卓越的新一代Web框架 Django的处理流程 1. 下载地址 Python 下载地址:https://www.pyt ...
- Python Flask Web 框架入门
Python Flask 目录 本文主要借鉴 letiantian 的文章 http://www.letiantian.me/learn-flask/ 一.简介 二.安装 三.初始化Flask 四.获 ...
- 比我的脸还干的gan货——Python Flask Web 框架入门
Flask是一个轻量级的基于Python的web框架. 本文适合有一定HTML.Python.网络基础的同学阅读. 1. 简介 这份文档中的代码使用 Python 3 运行.是的,所以读者需要自己在电 ...
- Python之Web框架
Python之Web框架: 一. Web框架的本质: 对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env pyth ...
- python 实现web框架simfish
python 实现web框架simfish 本文主要记录本人利用python实现web框架simfish的过程.源码github地址:simfish WSGI HTTP Server wsgi模块提供 ...
- Python之Web框架们
Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. pip i ...
- python各种web框架对比
0 引言 python在web开发方面有着广泛的应用.鉴于各种各样的框架,对于开发者来说如何选择将成为一个问题.为此,我特此对比较常见的几种框架从性能.使用感受以及应用情况进行一个粗略的 ...
- python之web框架(2):了解WSGI接口
python之web框架(2):了解WSGI接口 1.什么是wsgi接口: wsgi:Web Service Gateway Interface.它不是模块,而只是一种规范,方便web服务器和各种框架 ...
- python之web框架(1):完成静态页面web服务器
python的web框架(1) 1.首先写一个最简单的web服务器,只能给客户回应一个固定的hello world的页面. from socket import * from multiprocess ...
- Python的WEB框架
Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. ? 1 2 ...
随机推荐
- Apache Beam是什么?
Apache Beam 的前世今生 1月10日,Apache软件基金会宣布,Apache Beam成功孵化,成为该基金会的一个新的顶级项目,基于Apache V2许可证开源. 2003年,谷歌发布了著 ...
- google protobuf VC下的使用笔记
1 使用protobuf 2.x 下载地址(3.x 在c++11 vs2017下报错) 源码 https://github.com/google/protobuf 或者直接下载 二进制文件 2 如果下 ...
- Netsharp配置文件
一.总体说明 netsharp下需要配置的项目一般是需要独立启动的项目,主要有四个 netsharp-web netsharp-test netsharp-elephant netsharp-donk ...
- 【并发编程】IO模型
一.要点回顾 为了更好地了解IO模型,我们需要先回顾下几个概念:同步.异步.阻塞.非阻塞 同步: 一个进程在执行某个任务时,另外一个进程必须等待其执行完毕,才能继续执行.就是在发出一个功能调用时,在没 ...
- 微服务架构day01
1.微服务架构的基本概念 分布式:将一个项目模块化 区分为多个子项目(自己理解:将业务逻辑层和数据库访问层独立化 通过rpc远程调用(rpc框架 springCould httpCliend ...
- 2019.02.11 bzoj4767: 两双手(组合数学+容斥dp)
传送门 题意简述:你要从(0,0)(0,0)(0,0)走到(ex,ey)(ex,ey)(ex,ey),每次可以从(x,y)(x,y)(x,y)走到(x+ax,y+ay)(x+ax,y+ay)(x+ax ...
- abaqus查询网格相关
怎样查询网格中总共有多少单元和节点: 可以进入mesh模块----tools----query----mesh----下面选择select elements----选中所有单元----鼠标中键就可以显 ...
- .NET 4.0中的泛型逆变和协变
转载自:http://www.cnblogs.com/Ninputer/archive/2008/11/22/generic_covariant.html:自己加了一些理解 随Visual Studi ...
- Reading | 《C++ Primer Plus》(未完待续)
目录 一.概述和C++简史 1.早期语言的问题 2.面向对象编程OOP 3.泛型编程 二.入门 1.头文件 2.名称空间 3.cout输出 4.C++语句 5.函数 一.概述和C++简史 C++融合了 ...
- JSP的分页技术
在实际应用中,如果从数据库中查询的记录特别的多,甚至超过了显示屏的显示范围,这个时候可将结果进行分页显示. 假设总记录数为intRowCount,每页显示的数量为inPageSize,总页数为intP ...