English 中文(简体)
改写路标有固定表达形式
原标题:Rewriting An URL With Regular Expression Substitution in Routes

在我的笔标书中,有些内容位于URLs,如。 http://mysite/data/31415。 用户可以通过搜索网页直接进入URL,或搜索“31415”。 然而,我的制约因素意味着。 http://mysite/data/000031415。 查阅“00000000000031415” Can 我在路道上从这种扼杀中 leading出零,或者我是否需要在控制器档案中作这种替代? 如果能够这样做的话。 简而言之,我是这样做的,但从我读到的文件中,我可以略微看出这一点。

最佳回答

实际上,你可以通过conditionalFunction,因为这些功能使你能够更改《URL》现有的变量。

问题回答

我知道,我正在通过引进一个不同的路线图书馆,因为我用的是<代码>Routes,但这里使用的是<代码>Werkzeug的路程式包。 它请你具体指出,一条道路是一片 in。 如果你想让更多的人(例如UUID)感到兴趣的话,你也可以通过继承<条码>werkzeug.routing.BaseConver<>/code”实施更专门的“converter”。

也许,<代码>Routes有一个类似的机制,用于满足特殊道路离距需要。

import unittest
from werkzeug.routing import Map, Rule

class RoutingWithInts(unittest.TestCase):
    m = Map([Rule( /data/<int:record_locator> , endpoint= data_getter )])

    def test_without_leading_zeros(self):
        urls = self.m.bind( localhost )
        endpoint, urlvars = urls.match( /data/31415 )
        self.assertEquals({ record_locator : 31415}, urlvars)

    def test_with_leading_zeros(self):
        urls = self.m.bind( localhost )
        endpoint, urlvars = urls.match( /data/000031415 )
        self.assertEquals({ record_locator : 31415}, urlvars)

unittest.main()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签