当前位置:网站首页>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 .
边栏推荐
- [disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
- Pan for in-depth understanding of the attention mechanism in CV
- FBI警告:有人利用AI换脸冒充他人身份进行远程面试
- Dynamic planning -- expansion topics
- Why should the gradient be manually cleared before back propagation in pytorch?
- 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
- 【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
- Driveseg: dynamic driving scene segmentation data set
- Record: install MySQL on ubuntu18.04
- 我眼中真正优秀的CTO长啥样
猜你喜欢

Record the errors reported when running fluent in the simulator

DriveSeg:动态驾驶场景分割数据集
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】

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

Flutter网络和数据存储框架搭建 -b1
![leetcode:556. Next larger element III [simulation + change as little as possible]](/img/a0/12e5ee5d01d666acb4b75ada2e6fec.png)
leetcode:556. Next larger element III [simulation + change as little as possible]
![[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难

FBI warning: some people use AI to disguise themselves as others for remote interview
![[wallpaper] (commercially available) 70 wallpaper HD free](/img/21/6802da1056a18157b15de85df60cf5.jpg)
[wallpaper] (commercially available) 70 wallpaper HD free

Nous avons fait une plateforme intelligente de règlement de détail
随机推荐
2022.2.14 Li Kou - daily question - single element in an ordered array
php-fpm的max_chindren的一些误区
硬盘监控和分析工具:Smartctl
Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
Le changement est un thème éternel
shell 脚本中关于用户输入参数的处理
DriveSeg:动态驾驶场景分割数据集
Analyse du Code du planificateur ego bspline Section Optimizer (1)
The most valuable thing
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
01. Preparation for automated office (free guidance, only three steps)
Flutter network and data storage framework construction-b1
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
High concurrency Architecture - distributed search engine (ES)
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
Why should the gradient be manually cleared before back propagation in pytorch?
變化是永恒的主題
Using the visualization results, click to appear the corresponding sentence
“google is not defined” when using Google Maps V3 in Firefox remotely