librelist archives

« back to archive

Automatching ReWrite rules in Flask like Wordpress

Automatching ReWrite rules in Flask like Wordpress

From:
cbrueggenolte
Date:
2013-02-14 @ 08:37
Good Morning and greetings from Germany!

Currently I am using the "mynt" framework from Andrew Fricke and I am
kinda happy with it. But to get more familier with web development and
with python I was asking my self if I can build a "small" wordpress. I
did the tutorial and it is working fine. But what I don't know is, if
I can do this ReWrite on the URIs.

In Wordpress you can enter "http://website.com/2012/hello" and it will
open the first matching URI, for example:
"http://website.com/2012/hello-world-from-wordpress"

Is it possible to have a rout like this in flask? Something with
regular expressions maybe.

Thanks

v/r
Carsten

Re: [flask] Automatching ReWrite rules in Flask like Wordpress

From:
Nathan M
Date:
2013-02-14 @ 08:53
Hi,

You could hook on the 404 exception, and look for a matching url.
Le 14 févr. 2013 09:44, "cbrueggenolte" <cbrueggenolte@gmail.com> a écrit :

> Good Morning and greetings from Germany!
>
> Currently I am using the "mynt" framework from Andrew Fricke and I am
> kinda happy with it. But to get more familier with web development and
> with python I was asking my self if I can build a "small" wordpress. I
> did the tutorial and it is working fine. But what I don't know is, if
> I can do this ReWrite on the URIs.
>
> In Wordpress you can enter "http://website.com/2012/hello" and it will
> open the first matching URI, for example:
> "http://website.com/2012/hello-world-from-wordpress"
>
> Is it possible to have a rout like this in flask? Something with
> regular expressions maybe.
>
> Thanks
>
> v/r
> Carsten
>

Re: [flask] Automatching ReWrite rules in Flask like Wordpress

From:
Markus Unterwaditzer
Date:
2013-02-14 @ 08:51
cbrueggenolte <cbrueggenolte@gmail.com> wrote:

>Good Morning and greetings from Germany!
>
>Currently I am using the "mynt" framework from Andrew Fricke and I am
>kinda happy with it. But to get more familier with web development and
>with python I was asking my self if I can build a "small" wordpress. I
>did the tutorial and it is working fine. But what I don't know is, if
>I can do this ReWrite on the URIs.
>
>In Wordpress you can enter "http://website.com/2012/hello" and it will
>open the first matching URI, for example:
>"http://website.com/2012/hello-world-from-wordpress"
>
>Is it possible to have a rout like this in flask? Something with
>regular expressions maybe.
>
>Thanks
>
>v/r
>Carsten

That's up to you. Flask doesn't have a feature like this, you're supposed 
to do it yourself.

If you have a route like "/<int:year>/<title>", you probably could query 
the posts that start with ``title`` from whatever you're using as a model 
and use flask.redirect to redirect to the proper URL if the title is 
incomplete, 404 if there is not exactly one matching post.
-- Markus

Re: [flask] Automatching ReWrite rules in Flask like Wordpress

From:
cbrueggenolte
Date:
2013-02-14 @ 09:02
Thanks Markus,

thanks foryour reply. I will try that inside the function then.
Is there something like a "catch-all" route?



2013/2/14 Markus Unterwaditzer <markus@unterwaditzer.net>:
> cbrueggenolte <cbrueggenolte@gmail.com> wrote:
>
>>Good Morning and greetings from Germany!
>>
>>Currently I am using the "mynt" framework from Andrew Fricke and I am
>>kinda happy with it. But to get more familier with web development and
>>with python I was asking my self if I can build a "small" wordpress. I
>>did the tutorial and it is working fine. But what I don't know is, if
>>I can do this ReWrite on the URIs.
>>
>>In Wordpress you can enter "http://website.com/2012/hello" and it will
>>open the first matching URI, for example:
>>"http://website.com/2012/hello-world-from-wordpress"
>>
>>Is it possible to have a rout like this in flask? Something with
>>regular expressions maybe.
>>
>>Thanks
>>
>>v/r
>>Carsten
>
> That's up to you. Flask doesn't have a feature like this, you're 
supposed to do it yourself.
>
> If you have a route like "/<int:year>/<title>", you probably could query
the posts that start with ``title`` from whatever you're using as a model 
and use flask.redirect to redirect to the proper URL if the title is 
incomplete, 404 if there is not exactly one matching post.
> -- Markus

Re: [flask] Automatching ReWrite rules in Flask like Wordpress

From:
Markus Unterwaditzer
Date:
2013-02-14 @ 09:44
cbrueggenolte <cbrueggenolte@gmail.com> wrote:

>Thanks Markus,
>
>thanks foryour reply. I will try that inside the function then.
>Is there something like a "catch-all" route?
>
>
>
>2013/2/14 Markus Unterwaditzer <markus@unterwaditzer.net>:
>> cbrueggenolte <cbrueggenolte@gmail.com> wrote:
>>
>>>Good Morning and greetings from Germany!
>>>
>>>Currently I am using the "mynt" framework from Andrew Fricke and I am
>>>kinda happy with it. But to get more familier with web development
>and
>>>with python I was asking my self if I can build a "small" wordpress.
>I
>>>did the tutorial and it is working fine. But what I don't know is, if
>>>I can do this ReWrite on the URIs.
>>>
>>>In Wordpress you can enter "http://website.com/2012/hello" and it
>will
>>>open the first matching URI, for example:
>>>"http://website.com/2012/hello-world-from-wordpress"
>>>
>>>Is it possible to have a rout like this in flask? Something with
>>>regular expressions maybe.
>>>
>>>Thanks
>>>
>>>v/r
>>>Carsten
>>
>> That's up to you. Flask doesn't have a feature like this, you're
>supposed to do it yourself.
>>
>> If you have a route like "/<int:year>/<title>", you probably could
>query the posts that start with ``title`` from whatever you're using as
>a model and use flask.redirect to redirect to the proper URL if the
>title is incomplete, 404 if there is not exactly one matching post.
>> -- Markus

"/<path:url>" would include any slashes in ``url``. Flask has also a thing
that ensures this wouldn't override all other routes, but i'd still use it
with caution.
-- Markus

Re: [flask] Automatching ReWrite rules in Flask like Wordpress

From:
phryk
Date:
2013-02-14 @ 09:43
On Thu, 14 Feb 2013 10:02:25 +0100
cbrueggenolte <cbrueggenolte@gmail.com> wrote:

> Is there something like a "catch-all" route?

http://flask.pocoo.org/docs/api/#url-route-registrations

You can use the path converter like this:

@app.route('/<path:p>')
def foo(p):
  return "path is %s" % p