博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[python]pyramid 学习4 (views)
阅读量:5788 次
发布时间:2019-06-18

本文共 1576 字,大约阅读时间需要 5 分钟。

Defining a View Callable as a Function

from pyramid.response import Response def hello_world(request): return Response('Hello world!')

 

Defining a View Callable as a Class

from pyramid.response import Response class MyView(object): def __init__(self, request):         self.request = request def __call__(self): return Response('hello')

 

HTTP异常 (pyramid.httpexceptions)

抛出401的第一种方法 

from pyramid.httpexceptions import HTTPUnauthorized def aview(request): raise HTTPUnauthorized()

抛出401的第二种方法

from pyramid.httpexceptions import exception_response def aview(request): raise exception_response(401)

 

403 pyramid.httpexceptions.HTTPForbidden. 404 pyramid.httpexceptions.HTTPNotFound

 

自定义异常

class ValidationFailure(Exception): def __init__(self, msg):         self.msg = msg from pyramid.view import view_config from helloworld.exceptions import ValidationFailure @view_config(context=ValidationFailure) def failed_validation(exc, request):     response =  Response('Failed validation: %s' % exc.msg)     response.status_int = 500 return response

 

header重定向

from pyramid.httpexceptions import HTTPFound def myview(request): return HTTPFound(location='http://example.com')

 

获取POST数据

def myview(request):     firstname = request.params['firstname']     lastname = request.params['lastname'] def myview(request): # the .decode('utf-8') will break below if there are any high-order     # characters in the firstname or lastname     firstname = request.params['firstname'].decode('utf-8')     lastname = request.params['lastname'].decode('utf-8')

 

复习一下 route中获取数据(GET)

request.matchdict['xxxx']

 

转载地址:http://cmlyx.baihongyu.com/

你可能感兴趣的文章
数据解析1:XML解析(1)
查看>>
bootstrap treetable 树形网格,动态扩展,连数据库
查看>>
生成器与迭代器
查看>>
Windows环境下JDK安装与环境变量配置详细的图文教程
查看>>
最长上升子序列模板 hdu 1087 Super Jumping! Jumping! Jumping!
查看>>
常用binlog日志操作命令
查看>>
VS2017 WinFrom打包设置与教程
查看>>
IOS Animation-KeyPath值
查看>>
docker初步
查看>>
TFT LCD显示原理详解
查看>>
php pcntl 多进程学习
查看>>
Django系列教程:三、动态视图和动态Url
查看>>
HTML5 canvas生成图片马赛克特效插件
查看>>
poj 2057 树形DP,数学期望
查看>>
oracle 创建job
查看>>
java 事件机制
查看>>
cmd命令 sc
查看>>
CentOS 6.3下NFS安装配置
查看>>
zabbix系列(六)zabbix添加对ubuntu系统的监控
查看>>
oracle学习笔记(4)
查看>>