librelist archives

« back to archive

Regexp in @app.route

Regexp in @app.route

From:
Lix Xu
Date:
2010-05-19 @ 03:00
Hi,
     I wonder how to get below things work:
     The instructure of my app is:
     myapp
         /myapp/
            __init__.py
            /static/
                /images/
                /css/
                /javascripts/
            /templates/
                /users/
                    index.html
                layout.html
                _formhelpers.html
            /models/
                user.py
            /views/
                users.py

     # a method to get user avatar from mongodb gridfs
     @app.route('/get_avatar/<oid>')
         def get_avatar(oid):
             if oid is None:
                 return g.fs.get_last_version('avatar').read()
             return g.fs.get(ObjectId(oid)).read()

    # and in the html
    <img src="{{ url_for('.get_avatar', oid = g.user['avatar'] }}"></img>

    # if g.user['avatar'] is None (like anonymous) it will get "
werkzeug.routing.BuildError BuildError: ('get_avatar', {}, None)"

    # I look through the flask.route but still have no idea how to fix it.

    *Is there any way to allow the url use None?*
*
*
*    *Any suggestions will be appreciate
*
*
*Lix Xu*

Re: Regexp in @app.route

From:
Lix Xu
Date:
2010-05-19 @ 03:12
After read the werkzeug/routing.py, and it will ignore the values if the
value of the values is None.
Is there any way to get my code run?
Thanks.

Lix Xu

On Wed, May 19, 2010 at 11:00 AM, Lix Xu <xuzenglin@gmail.com> wrote:

> Hi,
>      I wonder how to get below things work:
>      The instructure of my app is:
>      myapp
>          /myapp/
>             __init__.py
>             /static/
>                 /images/
>                 /css/
>                 /javascripts/
>             /templates/
>                 /users/
>                     index.html
>                 layout.html
>                 _formhelpers.html
>             /models/
>                 user.py
>             /views/
>                 users.py
>
>      # a method to get user avatar from mongodb gridfs
>      @app.route('/get_avatar/<oid>')
>          def get_avatar(oid):
>              if oid is None:
>                  return g.fs.get_last_version('avatar').read()
>              return g.fs.get(ObjectId(oid)).read()
>
>     # and in the html
>     <img src="{{ url_for('.get_avatar', oid = g.user['avatar'] }}"></img>
>
>     # if g.user['avatar'] is None (like anonymous) it will get "
> werkzeug.routing.BuildError BuildError: ('get_avatar', {}, None)"
>
>     # I look through the flask.route but still have no idea how to fix it.
>
>     *Is there any way to allow the url use None?*
> *
> *
> *    *Any suggestions will be appreciate
> *
> *
> *Lix Xu*
>

Re: [flask] Re: Regexp in @app.route

From:
Armin Ronacher
Date:
2010-05-19 @ 05:06
On 2010-05-19 5:12 AM, Lix Xu wrote:
> After read the werkzeug/routing.py, and it will ignore the values if the
> value of the values is None.
> Is there any way to get my code run?
Break it up in two rules:

     @app.route('/get_avatar/<oid>')
     @app.route('/get_avatar/last', defaults={'oid': None})
     def get_avatar(oid):
         if oid is None:
             return g.fs.get_last_version('avatar').read()
         return g.fs.get(ObjectId(oid)).read()

That way if you do not provide 'oid' it will generate the URL 
/get_avatar/last and pass None as oid, and will match oid as None for 
/get_avatar/last.  Alternatively you can of course set the URL for the 
defaulted route to just /get_avatar/.

Keep in mind that variable parts never fall back to an empty string. 
They have to be at least one char in size, so not only URL generation 
fails, also matching in the code you had.


Regards,
Armin

Re: [flask] Re: Regexp in @app.route

From:
Lix Xu
Date:
2010-05-19 @ 09:04
Thanks, it  can work now. But I have doubt that:
Usually, the paths /login/ and /login should be the same.
For example, if I set route in the view method like:

@users.route('/login', methods = ['GET', 'POST'])
def login():
    # lines of codes

When I access http://localhost:5000/login/, it will raise 404 error.
So I have to add:
@users.route('/login/', methods = ['GET', 'POST'])
And, '/login/' must be above '/login', or it'll redirect to /login again and
again.
The full is like, the order is important (why):
@users.route('/login/', methods = ['GET', 'POST'])
@users.route('/login', methods = ['GET', 'POST'])
def login():
    # lines of codes

I have no idea why the order is required.

I have the concept that if wen can write the url like '/login/?' where '?'
means 0 or 1 like Python regexp.
Whether to match /login and /login/, the below is required.
@users.route('/login/', methods = ['GET', 'POST'])
@users.route('/login', methods = ['GET', 'POST'])

Lix Xu

On Wed, May 19, 2010 at 1:06 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> On 2010-05-19 5:12 AM, Lix Xu wrote:
> > After read the werkzeug/routing.py, and it will ignore the values if the
> > value of the values is None.
> > Is there any way to get my code run?
> Break it up in two rules:
>
>     @app.route('/get_avatar/<oid>')
>      @app.route('/get_avatar/last', defaults={'oid': None})
>      def get_avatar(oid):
>         if oid is None:
>             return g.fs.get_last_version('avatar').read()
>         return g.fs.get(ObjectId(oid)).read()
>
> That way if you do not provide 'oid' it will generate the URL
> /get_avatar/last and pass None as oid, and will match oid as None for
> /get_avatar/last.  Alternatively you can of course set the URL for the
> defaulted route to just /get_avatar/.
>
> Keep in mind that variable parts never fall back to an empty string.
> They have to be at least one char in size, so not only URL generation
> fails, also matching in the code you had.
>
>
> Regards,
> Armin
>

Re: [flask] Re: Regexp in @app.route

From:
Armin Ronacher
Date:
2010-05-19 @ 09:39
Hi,

On 5/19/10 11:04 AM, Lix Xu wrote:
> Thanks, it  can work now. But I have doubt that:
> Usually, the paths /login/ and /login should be the same.
> For example, if I set route in the view method like:
Usually?  What is usually.  Flask/Werkzeug follow Apache rules for URL 
definitions.

Folders (/foo/) can be accessed as /foo and redirect to /foo/, files or 
leaves (/foo) do not redirect but result in 404.  That is consistent and 
enables relative URLs and keeps them unique.

You can try that for yourself here (standard Apache):

    http://dev.pocoo.org/~mitsuhiko
        redirects to
    http://dev.pocoo.org/~mitsuhiko/


    http://dev.pocoo.org/~mitsuhiko/austrALia.jpeg
       displays the file but
    http://dev.pocoo.org/~mitsuhiko/austrALia.jpeg/
       results in 404.

That standard was established a long ago, I don't see why that should be 
changed.


Regards,
Armin

Re: [flask] Re: Regexp in @app.route

From:
Lix Xu
Date:
2010-05-19 @ 12:43
Hi Armin,
   Thanks for warm support.
   The 'usually' here means /login and /login/ should be both the correct
url and bote point to the same place while user access the website. I just
want to catch all the exceptions by user wrong typing or the wrong path.
   For the truely wrong path, I can redirect the user to 404 page via
@app.errorhandler(404).
But for the paths such as /login/ and /login, they are both correct paths
and don't result in 404 for /login/.
   Now,
@app.route('/login/', ...)
@app.route('/login', ...)
def login():
    #..

this way can be work well for me.
    I just want to find a better way to define the routing, beautiful and
clean.
    This way is the best way for me now.
    Thank you very much.
    By the way, flask is awesome.

Lix Xu

On Wed, May 19, 2010 at 5:39 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 5/19/10 11:04 AM, Lix Xu wrote:
> > Thanks, it  can work now. But I have doubt that:
> > Usually, the paths /login/ and /login should be the same.
> > For example, if I set route in the view method like:
> Usually?  What is usually.  Flask/Werkzeug follow Apache rules for URL
> definitions.
>
> Folders (/foo/) can be accessed as /foo and redirect to /foo/, files or
> leaves (/foo) do not redirect but result in 404.  That is consistent and
> enables relative URLs and keeps them unique.
>
> You can try that for yourself here (standard Apache):
>
>    http://dev.pocoo.org/~mitsuhiko <http://dev.pocoo.org/%7Emitsuhiko>
>        redirects to
>    http://dev.pocoo.org/~mitsuhiko/ <http://dev.pocoo.org/%7Emitsuhiko/>
>
>
>    
http://dev.pocoo.org/~mitsuhiko/austrALia.jpeg<http://dev.pocoo.org/%7Emitsuhiko/austrALia.jpeg>
>       displays the file but
>    
http://dev.pocoo.org/~mitsuhiko/austrALia.jpeg/<http://dev.pocoo.org/%7Emitsuhiko/austrALia.jpeg/>
>       results in 404.
>
> That standard was established a long ago, I don't see why that should be
> changed.
>
>
> Regards,
> Armin
>

Re: [flask] Re: Regexp in @app.route

From:
Armin Ronacher
Date:
2010-05-19 @ 12:53
Hi,

On 5/19/10 2:43 PM, Lix Xu wrote:
>      I just want to find a better way to define the routing, beautiful
> and clean.
Then just do this:

    @app.route('/login/')

(note the trailing slash)


Regards,
Armin

Re: [flask] Re: Regexp in @app.route

From:
Lix Xu
Date:
2010-05-19 @ 13:52
Thanks, I found the problem why it redirect to /login again and again.
I use <form method="post" action="/login"> in the form.
After I changed the action to action="{{ url_for('users.login') }}", it
works prefect.
Thanks again, I'm too careless.
Now in the view method, I can just define it as @app.route('/login/') as you
said, it's ok now.

Lix Xu

On Wed, May 19, 2010 at 8:53 PM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 5/19/10 2:43 PM, Lix Xu wrote:
> >      I just want to find a better way to define the routing, beautiful
> > and clean.
> Then just do this:
>
>    @app.route('/login/')
>
> (note the trailing slash)
>
>
> Regards,
> Armin
>

Re: [flask] Re: Regexp in @app.route

From:
Stephane Wirtel
Date:
2010-05-19 @ 05:36
hi all,

just for info, there is the concept of regexp for the routes in the  
bottle fremework.

regards

Written from my iPhone !

Le 19 mai 2010 à 07:06, Armin Ronacher <armin.ronacher@active-4.com> a  
écrit :

> On 2010-05-19 5:12 AM, Lix Xu wrote:
>> After read the werkzeug/routing.py, and it will ignore the values  
>> if the
>> value of the values is None.
>> Is there any way to get my code run?
> Break it up in two rules:
>
>     @app.route('/get_avatar/<oid>')
>     @app.route('/get_avatar/last', defaults={'oid': None})
>     def get_avatar(oid):
>         if oid is None:
>             return g.fs.get_last_version('avatar').read()
>         return g.fs.get(ObjectId(oid)).read()
>
> That way if you do not provide 'oid' it will generate the URL
> /get_avatar/last and pass None as oid, and will match oid as None for
> /get_avatar/last.  Alternatively you can of course set the URL for the
> defaulted route to just /get_avatar/.
>
> Keep in mind that variable parts never fall back to an empty string.
> They have to be at least one char in size, so not only URL generation
> fails, also matching in the code you had.
>
>
> Regards,
> Armin

Re: [flask] Re: Regexp in @app.route

From:
Armin Ronacher
Date:
2010-05-19 @ 06:16
Hi,

On 5/19/10 7:36 AM, Stephane Wirtel wrote:
> just for info, there is the concept of regexp for the routes in the
> bottle fremework.
Regexps are not 100% reversible because there can be rules outside of 
groups.  Furthermore they do not indicate priority which makes it hard 
to define rules out of order.

So that's something I do not want to do.  You can implement your own 
converters with different regular expressions though.


Regards,
Armin