当前位置:网站首页>Pecan - route
Pecan - route
2022-07-03 19:14:00 【Procedural ape treading on colorful auspicious clouds】
List of articles
object-dispatch
Pecan Use object-dispatch
The routing policy will HTTP The request is mapped to a controller.
This strategy uses /
Split into lists , Then from the root controller Start looking for the corresponding layer by layer controller.
Example :
Online store pair /catalog/books/bestsellers
The requests are divided into catalog,books and bestsellers. Next ,Pecan Will find on the root controller catalog. And then in catalog Object books, In the end in books Find bestsellers Method .
from pecan import expose
class BooksController(object):
@expose()
def index(self):
return "Welcome to book section."
@expose()
def bestsellers(self):
return "We have 5 books in the top 10."
class CatalogController(object):
@expose()
def index(self):
return "Welcome to the catalog."
books = BooksController()
class RootController(object):
@expose()
def index(self):
return "Welcome to store.example.com!"
@expose()
def hours(self):
return "Open 24/7 on the web."
catalog = CatalogController()
Sometimes , Standard object dispatch routing is not sufficient to URL Route to the controller correctly .Pecan Several methods are provided to simplify the object dispatch system , To deal with... With more control url, Including special _lookup()、_default() and _route() Method . These methods can be defined on the controller object to handle all or part of URL Provide additional flexibility .
_lookup
_lookup() Special methods provide a way to deal with URL Part of , Then return a new controller object to route to the rest .
_lookup() Method can accept one or more parameters , I.e. to be handled URL Path segment ( Split into /)._lookup() You should also accept variable location parameters that represent the rest of the path , And should include any part of the path that it does not handle in the return value . The following example uses a *remainder list , This list will be passed to the returning controller when the object scheduling algorithm continues .
In addition to being used to dynamically create controllers , When no other controller method matches URL And there's no _default() When the method is used ,_lookup() Called as a last resort .
Example :
HTTP GET /8/name
The request will return primary_key == 8
The name of the student .
from pecan import expose, abort
from somelib import get_student_by_name
class StudentController(object):
def __init__(self, student):
self.student = student
@expose()
def name(self):
return self.student.name
class RootController(object):
@expose()
def _lookup(self, primary_key, *remainder):
student = get_student_by_primary_key(primary_key)
if student:
return StudentController(student), remainder
else:
abort(404)
_default
When the request path does not match the corresponding method ,_default Will be called .
Example :
/spanish
Will be routed to RootController._default()
from pecan import expose
class RootController(object):
@expose()
def english(self):
return 'hello'
@expose()
def french(self):
return 'bonjour'
@expose()
def _default(self):
return 'I cannot say hello in that language'
_route
_route()
Method allows the controller to completely cover Pecan The routing mechanism of .Pecan Self use _route()
Method to realize its RestController
. If you want to Pecan Design an alternative routing system , Then define a definition _route()
The basic controller class of the method will give you full control .
边栏推荐
- [mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
- Free year-end report summary template Welfare Collection
- 【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
- Does SQL always report foreign key errors when creating tables?
- Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
- [optics] vortex generation based on MATLAB [including Matlab source code 1927]
- Scrape crawler framework
- Streaming media server (16) -- figure out the difference between live broadcast and on-demand
- Record: MySQL changes the time zone
- The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
猜你喜欢
2022.2.14 Li Kou - daily question - single element in an ordered array
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
我眼中真正优秀的CTO长啥样
为什么要做特征的归一化/标准化?
Analysis of dart JSON encoder and decoder
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
Free year-end report summary template Welfare Collection
【LeetCode】【SQL】刷题笔记
FBI warning: some people use AI to disguise themselves as others for remote interview
随机推荐
硬盘监控和分析工具:Smartctl
Today I am filled with emotion
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
01. Preparation for automated office (free guidance, only three steps)
Thesis study - 7 Very Deep Convolutional Networks for Large-Scale Image Recognition (3/3)
[leetcode] [SQL] notes
leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
达梦数据库的物理备份和还原简解
记录在模拟器中运行flutter时报的错
Add control at the top of compose lazycolumn
Flask generates swagger documents
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
During MySQL installation, the download interface is empty, and the components to be downloaded are not displayed. MySQL installer 8.0.28.0 download interface is empty solution
SQL: special update operation
利用可视化结果,点击出现对应的句子
【LeetCode】【SQL】刷题笔记
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
Yolov3 network model building
math_ Taylor formula