当前位置:网站首页>Pecan — @expose()
Pecan — @expose()
2022-07-03 19:14:00 【Procedural ape treading on colorful auspicious clouds】
List of articles
@expose()
pecan.expose Can mark controller Method , So that this method can pass http visit .
pecan.expose The definition is as follows :
pecan.expose(template=None, generic=False, route=None, **kw)
- template: The path of the template . It can also be a renderer represented by strings , for example ‘json’.
- content_type: by template Appoint content-type
- generic:boolean value , When it comes to true when , The same path will result from http Different methods are routed to the corresponding methods .
- route: Specify the name of the path segment . The default is the function name .
Example :
hello Method will return Hello World As HTML Response body
from pecan import expose
class RootController(object):
@expose()
def hello(self):
return 'Hello World'
expose() You can use the same method multiple times .
from pecan import expose
class RootController(object):
@expose('json')
@expose('text_template.mako', content_type='text/plain')
@expose('html_template.mako')
def hello(self):
return {
'msg': 'Hello!'}
<!-- html_template.mako -->
<html>
<body>${msg}</body>
</html>
@expose(‘json’) Will use JSON Serialize the return value , The request path is /hello.json
@expose(‘text_template.mako’, content_type=‘text/plain’) Will use text_template.mako Rendering , The request path is /hello.txt
@expose(‘html_template.mako’) The request path is /hello.html, Default content-type by text/html
Specify path segments
If you want to use include - The path of , for example /some-path, But in Python in , Method names cannot contain -. At this point, you can specify expose Of route value .
Example :
Only through /some-path Path successfully accessed . and /some_path Will return 404
class RootController(object):
@pecan.expose(route='some-path')
def some_path(self):
return dict()
Routing based on request method
generic by true when , The same path can be routed to the corresponding method according to the request method .
as follows ,index Handle http GET /,index_POST Handle http POST /
expose Will generate f.when Decorator .
from pecan import expose
class RootController(object):
# HTTP GET /
@expose(generic=True, template='json')
def index(self):
return dict()
# HTTP POST /
@index.when(method='POST', template='json')
def index_POST(self, **kw):
uuid = create_something()
return dict(uuid=uuid)
边栏推荐
- Common PostgreSQL commands
- 我們做了一個智能零售結算平臺
- Understanding of database architecture
- The installation path cannot be selected when installing MySQL 8.0.23
- Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
- Think of new ways
- How to build an efficient information warehouse
- Analysis of dart JSON encoder and decoder
- [leetcode] [SQL] notes
- 【LeetCode】【SQL】刷题笔记
猜你喜欢

235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】

Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week

Record: pymysql is used in pycharm to connect to the database

Nous avons fait une plateforme intelligente de règlement de détail

2022.02.11

Counting from the East and counting from the West will stimulate 100 billion industries. Only storage manufacturers who dare to bite the "hard bone" will have more opportunities

【光学】基于matlab介电常数计算【含Matlab源码 1926期】

Recommend a GIF processing artifact less than 300K - gifsicle (free download)

SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)

东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
随机推荐
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
application
Work Measurement - 1
为什么要做特征的归一化/标准化?
PyTorch中在反向传播前为什么要手动将梯度清零?
HOW TO WRITE A DAILY LAB NOTE?
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
我们做了一个智能零售结算平台
Flutter network and data storage framework construction-b1
TFs and SVN [closed] - TFs vs SVN [closed]
Web Security (VII) specific process of authentication with session cookie scheme
What does a really excellent CTO look like in my eyes
C enum contains value - C enum contains value
Recommend a GIF processing artifact less than 300K - gifsicle (free download)
Free year-end report summary template Welfare Collection
SSM integration - joint debugging of front and rear protocols (list function, add function, add function status processing, modify function, delete function)
Free hand account sharing in September - [cream Nebula]
How to build an efficient information warehouse
[leetcode] [SQL] notes