Re: [flask] Question about routing
- From:
- Sebastien Estienne
- Date:
- 2011-01-20 @ 00:23
Unfortunately changing the URL format is not an option, it's a
migration and keeping them as-is is a requirement.
I thought it would be possible with werkzeug... but i don't see how to
NOT match a random string in the URL.
Sebastien Estienne
On Thu, Jan 20, 2011 at 01:07, Aaron Kavlie <akavlie@gmail.com> wrote:
> I'm not sure if Flask can do that or not. I actually enjoy Flask's simpler
> routing.
> This may not be the answer you want, but if it were me I'd consider a
> different URL scheme, something like this:
> /1234/random-title/ext1
> /4234/another-title/ext2
> the scheme you're considering doesn't seem very friendly, and underscores
> are bad for search engines.
>
> On Wed, Jan 19, 2011 at 5:02 PM, Sebastien Estienne
> <sebastien.estienne@gmail.com> wrote:
>>
>> Hello
>>
>> I want to do something simple with regexp base routing like in django,
>> but i didn't find the solution in either flask or werkzeug
>> documentation:
>>
>> I have URLs like this /1234_random_title.ext1 or /4234_another_title.ext2
>> and i only want to match the numeric id and the extension, this would
>> be something like this in django:
>> /(?P<my_id>\d+)_.*\.(?P<extension>\w)
>>
>> I don't see how to skip/ignore part of the url in Flask: ignoring
>> random_title and another_title.
>>
>> Thanx for helping,
>> Sebastien Estienne
>
>
>
> --
> freelance web services
> aaron.kavlie.net
>
Re: [flask] Question about routing
- From:
- Steven Kryskalla
- Date:
- 2011-01-20 @ 01:21
On Wed, Jan 19, 2011 at 7:23 PM, Sebastien Estienne
<sebastien.estienne@gmail.com> wrote:
> Unfortunately changing the URL format is not an option, it's a
> migration and keeping them as-is is a requirement.
>
> I thought it would be possible with werkzeug... but i don't see how to
> NOT match a random string in the URL.
>
You can make your own converter and add it to the map:
--
from werkzeug.routing import BaseConverter
import re
class SpecialConverter(BaseConverter):
regex = '(\d+)_.*\.(\w+)'
is_greedy = False
def to_python(self, value):
return re.match(self.regex, value).groups()
app.url_map.converters['specialfile'] = SpecialConverter
@app.route('/<specialfile:parts>')
def grab_old_file(parts):
return repr(parts)
---
If you request: /1234_random_title.ext1
You get this value passed in as 'parts': (u'1234', u'ext1')
-Steve