当前位置:网站首页>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 .
边栏推荐
- Why should the gradient be manually cleared before back propagation in pytorch?
- High concurrency Architecture - distributed search engine (ES)
- Zhengda futures news: soaring oil prices may continue to push up global inflation
- Analysis of dart JSON encoder and decoder
- 【LeetCode】【SQL】刷题笔记
- leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
- Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
- 东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
- cipher
- Processing of user input parameters in shell script
猜你喜欢
Dart JSON编码器和解码器剖析
User identity used by startup script and login script in group policy
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
Free hand account sharing in September - [cream Nebula]
FBI warning: some people use AI to disguise themselves as others for remote interview
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
These problems should be paid attention to in the production of enterprise promotional videos
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
Record: install MySQL on ubuntu18.04
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
随机推荐
my. INI file not found
Nous avons fait une plateforme intelligente de règlement de détail
Web3 credential network project galaxy is better than nym?
Zhengda futures news: soaring oil prices may continue to push up global inflation
[new year job hopping season] test the technical summary of interviewers' favorite questions (with video tutorials and interview questions)
php-fpm的max_chindren的一些误区
Understanding of database architecture
【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
C enum contains value - C enum contains value
EGO Planner代码解析bspline_optimizer部分(3)
Briefly describe the quantitative analysis system of services
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
Smart wax therapy machine based on STM32 and smart cloud
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
Analysis of dart JSON encoder and decoder
Valentine's Day - make an exclusive digital collection for your lover
OSPF - detailed explanation of stub area and full stub area
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