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 ...
随机推荐
- rpo攻击
0 什么是RPO攻击? RPO(Relative Path Overwrite)相对路径覆盖,是一种新型攻击技术,最早由Gareth Heyes在其发表的文章中提出.主要是利用浏览器的一些特性和部分服 ...
- 谷歌开源的一个BTREE实现 Go语言
// Copyright 2014 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "Licens ...
- jquery 判断 元素是否具有某个class
两种方法如下: 1.hasClass(‘classname’) 2.is(‘.classname’) 例子: 1.使用is(‘.classname’)的方法 $('div').is('.redColo ...
- solr7.7.0搜索引擎使用(四)(搜索语法)
solr搜索语法 参数defType 指定用于处理查询语句(参数q的内容)的查询解析器,eg:defType=lucenesort 指定响应的排序方式:升序asc或降序desc.同时需要指定 ...
- Java 8 Lambda 表达式及 Stream 在集合中的用法
简介 虽然 Java 8 已经发布有一段时间了,但是关于 Java 8 中的 Lambda 表达式最近才开始系统的学习,刚开始就被 Stream 的各种骚操作深深的吸引住了,简直漂亮的不像 Java. ...
- <fieldset>标签
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...
- LOJ-10108(欧拉回路+并查集)一个图至少用几笔画成
题目链接:传送门 思路: 用并查集统计出每个区块奇数个节点的个数x,每个区块对笔画的贡献是max(x/2,1): 然后每个区块求和即可. #include<iostream> #inclu ...
- PowerShell工作流学习-3-挂起工作流
关键点: a)可使用Suspend-Job或Suspend-Workflow(从工作流中)挂起工作流,无法从工作流中恢复工作流. 例a: Workflow Test-Suspend { $a = Ge ...
- 【pycharm 密钥】pycharm 2017 密钥
server选项里边输入: http://idea.liyang.io 亲测可用!!!
- Axure RP Xmind
官方网站下载地址:http://www.axure.com/download 下载地址:http://www.iaxure.com/2941.html 汉化安装:http://www.iaxure.c ...