当前位置:网站首页>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)
边栏推荐
猜你喜欢
Ego planner code parsing Bspline_ Optimizer section (2)
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
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
Recommend a simple browser tab
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
This Chinese numpy quick look-up table is too easy!
Why should we do feature normalization / standardization?
leetcode:556. Next larger element III [simulation + change as little as possible]
Day_ 18 IO stream system
Know what it is, and know why, JS object creation and inheritance [summary and sorting]
随机推荐
The most valuable thing
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
Foundation of ActiveMQ
【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】
东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
Record: install MySQL on ubuntu18.04
Zhengda futures news: soaring oil prices may continue to push up global inflation
Which do MySQL and Oracle learn?
Record: solve the problem that MySQL is not an internal or external command environment variable
DriveSeg:动态驾驶场景分割数据集
ActiveMQ的基础
Record: pymysql is used in pycharm to connect to the database
Ego planner code parsing Bspline_ Optimizer section (1)
Free sharing | linefriends hand account inner page | horizontal grid | not for sale
What does a really excellent CTO look like in my eyes
Think of new ways
Free hand account sharing in September - [cream Nebula]
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
“google is not defined” when using Google Maps V3 in Firefox remotely