您现在的位置是:主页 > news > 大莲网站建设公司/小程序如何推广运营

大莲网站建设公司/小程序如何推广运营

admin2025/5/7 6:43:16news

简介大莲网站建设公司,小程序如何推广运营,wordpress主题生成器,购物网站的名称和网址文章目录expose()指定路径段基于请求方法路由expose() pecan.expose可以标记controller方法,使得该方法可以通过http访问。 pecan.expose定义如下: pecan.expose(templateNone, genericFalse, routeNone, **kw)template:模板的路径。还可以…

大莲网站建设公司,小程序如何推广运营,wordpress主题生成器,购物网站的名称和网址文章目录expose()指定路径段基于请求方法路由expose() pecan.expose可以标记controller方法,使得该方法可以通过http访问。 pecan.expose定义如下: pecan.expose(templateNone, genericFalse, routeNone, **kw)template:模板的路径。还可以…

文章目录

    • @expose()
    • 指定路径段
    • 基于请求方法路由

@expose()

pecan.expose可以标记controller方法,使得该方法可以通过http访问。

pecan.expose定义如下:

pecan.expose(template=None, generic=False, route=None, **kw)
  • template:模板的路径。还可以是字符串表示的渲染器,例如 ‘json’。
  • content_type:为template指定content-type
  • generic:boolean值,当为true时,相同的路径会因http方法的不同路由到相应的方法。
  • route:指定路径段的名字。默认是函数名。

示例:

hello方法会返回Hello World作为HTML响应体

from pecan import exposeclass RootController(object):@expose()def hello(self):return 'Hello World'

expose()可以在同一个方法使用多次。

from pecan import exposeclass 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’)会用JSON序列化返回值,而请求路径为/hello.json

@expose(‘text_template.mako’, content_type=‘text/plain’)会使用text_template.mako进行渲染,请求路径为/hello.txt

@expose(‘html_template.mako’)请求路径为/hello.html,默认content-typetext/html

指定路径段

如果你想要使用包含-的路径,例如/some-path,但是在Python中,方法名不能包含-。此时可以指定exposeroute值。

示例:

此时只能通过/some-path路径成功访问。而/some_path将返回404

class RootController(object):@pecan.expose(route='some-path')def some_path(self):return dict()

基于请求方法路由

generic为true时,可以根据请求方法将相同的路径路由到相应的方法。

如下,index处理http GET /index_POST处理http POST /

expose会生成f.when装饰器。

from pecan import exposeclass 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)