librelist archives

« back to archive

Multiple variable parts in url

Multiple variable parts in url

From:
Jandos Khalik
Date:
2011-11-05 @ 01:16
The following doesn't work as I expect:

@app.route('/', defaults={'lang': 'en', 'func': 'index'})
@app.route('/<lang>/', defaults={'function': 'index'})
@app.route('/<func>', defaults={'lang': 'en'})
@app.route('/<lang>/<func>')
def url_handler(func):
        print func, g.lang
        return getattr(views, func)()

Can someone explain me what is the proper way?
Thank you.

Re: [flask] Multiple variable parts in url

From:
Ishbir Singh
Date:
2011-11-05 @ 03:50
Hey,

What do you mean by "doesn't work as expected"? What error do you get?

On 5 November 2011 06:46, Jandos Khalik <jandos.kh@gmail.com> wrote:

> The following doesn't work as I expect:
>
> @app.route('/', defaults={'lang': 'en', 'func': 'index'})
> @app.route('/<lang>/', defaults={'function': 'index'})
> @app.route('/<func>', defaults={'lang': 'en'})
> @app.route('/<lang>/<func>')
> def url_handler(func):
>         print func, g.lang
>         return getattr(views, func)()
>
> Can someone explain me what is the proper way?
> Thank you.
>



-- 
Regards,
Ishbir Singh

Re: [flask] Multiple variable parts in url

From:
Jandos Khalik
Date:
2011-11-05 @ 04:17
If I browse to http://127.0.0.1:5000/jp it says:
AttributeError: 'module' object has no attribute 'jp'
With trailing slash the error will be:
TypeError: url_handler() got an unexpected keyword argument 'function'

I think my intention is clear from decorators. But only '/' or full url
including
lang and func works. The other 2 have problem because when only lang is
passed the code assumes it as func etc.



On Sat, Nov 5, 2011 at 12:50 PM, Ishbir Singh <webmaster@ishbir.com> wrote:

> Hey,
>
> What do you mean by "doesn't work as expected"? What error do you get?
>
>
> On 5 November 2011 06:46, Jandos Khalik <jandos.kh@gmail.com> wrote:
>
>> The following doesn't work as I expect:
>>
>> @app.route('/', defaults={'lang': 'en', 'func': 'index'})
>> @app.route('/<lang>/', defaults={'function': 'index'})
>> @app.route('/<func>', defaults={'lang': 'en'})
>> @app.route('/<lang>/<func>')
>> def url_handler(func):
>>         print func, g.lang
>>         return getattr(views, func)()
>>
>> Can someone explain me what is the proper way?
>> Thank you.
>>
>
>
>
> --
> Regards,
> Ishbir Singh
>
>

Re: [flask] Multiple variable parts in url

From:
Simon Sapin
Date:
2011-11-05 @ 08:29
Le 05/11/2011 02:16, Jandos Khalik a écrit :
 > The following doesn't work as I expect:
 >
 > @app.route('/', defaults={'lang': 'en', 'func': 'index'})
 > @app.route('/<lang>/', defaults={'function': 'index'})
 > @app.route('/<func>', defaults={'lang': 'en'})
 > @app.route('/<lang>/<func>')
 > def url_handler(func):
 >          print func, g.lang
 >          return getattr(views, func)()

Le 05/11/2011 05:17, Jandos Khalik a écrit :
> If I browse to http://127.0.0.1:5000/jp it says:
> AttributeError: 'module' object has no attribute 'jp'
> With trailing slash the error will be:
> TypeError: url_handler() got an unexpected keyword argument 'function'
>

Hi,

Requesting /jp matches the third decorator, not the second. What do you 
get when requesting /jp/ (after changing function to func as Ishbir 
suggested) ?

Regards,
-- 
Simon Sapin

Re: [flask] Multiple variable parts in url

From:
Jandos Khalik
Date:
2011-11-05 @ 09:28
I have changed order of <func> and <lang>. Now the following works:

    @app.route('/', defaults={'func': 'index', 'lang': 'en'},
methods=['GET', 'POST'])
    @app.route('/<lang>/', defaults={'func': 'index'}, methods=['GET',
'POST'])
    @app.route('/<func>', defaults={'lang': 'en'}, methods=['GET', 'POST'])
    @app.route('/<func>/<lang>', methods=['GET', 'POST'])
    def url_handler(func):
          return getattr(views, func)()

I now think may be, /<lang>/ rule isn't nice. If user wants to see page
in a language other than english, just requiring full url seems to be
better.
Not related issue: all may urls will accept both GET and POST.
if calling function doesn't need posted data, it will be just ignored.
I don't know whether it is bad idea or not. Definitely, it is not RESTful.
Any comments welcome. Thanks to all again.



On Sat, Nov 5, 2011 at 5:29 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 05/11/2011 02:16, Jandos Khalik a écrit :
>  > The following doesn't work as I expect:
>  >
>  > @app.route('/', defaults={'lang': 'en', 'func': 'index'})
>  > @app.route('/<lang>/', defaults={'function': 'index'})
>  > @app.route('/<func>', defaults={'lang': 'en'})
>  > @app.route('/<lang>/<func>')
>  > def url_handler(func):
>  >          print func, g.lang
>  >          return getattr(views, func)()
>
> Le 05/11/2011 05:17, Jandos Khalik a écrit :
> > If I browse to http://127.0.0.1:5000/jp it says:
> > AttributeError: 'module' object has no attribute 'jp'
> > With trailing slash the error will be:
> > TypeError: url_handler() got an unexpected keyword argument 'function'
> >
>
> Hi,
>
> Requesting /jp matches the third decorator, not the second. What do you
> get when requesting /jp/ (after changing function to func as Ishbir
> suggested) ?
>
> Regards,
> --
> Simon Sapin
>

Re: [flask] Multiple variable parts in url

From:
Jandos Khalik
Date:
2011-11-05 @ 04:30
I should also say that I'm setting g.lang in
url preprocessor like in the documentation:

@app.url_value_preprocessor
def pull_lang(endpoint, values):
        if values:
            g.lang = values.pop('lang', None)
        else:
            g.lang = 'en'



On Sat, Nov 5, 2011 at 1:17 PM, Jandos Khalik <jandos.kh@gmail.com> wrote:

> If I browse to http://127.0.0.1:5000/jp it says:
> AttributeError: 'module' object has no attribute 'jp'
> With trailing slash the error will be:
>  TypeError: url_handler() got an unexpected keyword argument 'function'
>
> I think my intention is clear from decorators. But only '/' or full url
> including
> lang and func works. The other 2 have problem because when only lang is
> passed the code assumes it as func etc.
>
>
>
> On Sat, Nov 5, 2011 at 12:50 PM, Ishbir Singh <webmaster@ishbir.com>wrote:
>
>> Hey,
>>
>> What do you mean by "doesn't work as expected"? What error do you get?
>>
>>
>> On 5 November 2011 06:46, Jandos Khalik <jandos.kh@gmail.com> wrote:
>>
>>> The following doesn't work as I expect:
>>>
>>> @app.route('/', defaults={'lang': 'en', 'func': 'index'})
>>> @app.route('/<lang>/', defaults={'function': 'index'})
>>> @app.route('/<func>', defaults={'lang': 'en'})
>>> @app.route('/<lang>/<func>')
>>> def url_handler(func):
>>>         print func, g.lang
>>>         return getattr(views, func)()
>>>
>>> Can someone explain me what is the proper way?
>>> Thank you.
>>>
>>
>>
>>
>> --
>> Regards,
>> Ishbir Singh
>>
>>
>

Re: [flask] Multiple variable parts in url

From:
Ishbir Singh
Date:
2011-11-05 @ 06:21
Hey,

In your second route declaration, you've put <function> whereas it should
be <func>

On 5 November 2011 10:00, Jandos Khalik <jandos.kh@gmail.com> wrote:

> I should also say that I'm setting g.lang in
> url preprocessor like in the documentation:
>
> @app.url_value_preprocessor
> def pull_lang(endpoint, values):
>         if values:
>             g.lang = values.pop('lang', None)
>         else:
>             g.lang = 'en'
>
>
>
> On Sat, Nov 5, 2011 at 1:17 PM, Jandos Khalik <jandos.kh@gmail.com> wrote:
>
>> If I browse to http://127.0.0.1:5000/jp it says:
>> AttributeError: 'module' object has no attribute 'jp'
>> With trailing slash the error will be:
>>  TypeError: url_handler() got an unexpected keyword argument 'function'
>>
>> I think my intention is clear from decorators. But only '/' or full url
>> including
>> lang and func works. The other 2 have problem because when only lang is
>> passed the code assumes it as func etc.
>>
>>
>>
>> On Sat, Nov 5, 2011 at 12:50 PM, Ishbir Singh <webmaster@ishbir.com>wrote:
>>
>>> Hey,
>>>
>>> What do you mean by "doesn't work as expected"? What error do you get?
>>>
>>>
>>> On 5 November 2011 06:46, Jandos Khalik <jandos.kh@gmail.com> wrote:
>>>
>>>> The following doesn't work as I expect:
>>>>
>>>> @app.route('/', defaults={'lang': 'en', 'func': 'index'})
>>>> @app.route('/<lang>/', defaults={'function': 'index'})
>>>> @app.route('/<func>', defaults={'lang': 'en'})
>>>> @app.route('/<lang>/<func>')
>>>> def url_handler(func):
>>>>         print func, g.lang
>>>>         return getattr(views, func)()
>>>>
>>>> Can someone explain me what is the proper way?
>>>> Thank you.
>>>>
>>>
>>>
>>>
>>> --
>>> Regards,
>>> Ishbir Singh
>>>
>>>
>>
>


-- 
Regards,
Ishbir Singh

Re: [flask] Multiple variable parts in url

From:
Jandos Khalik
Date:
2011-11-05 @ 07:19
Sorry for stupid mistake. Nonetheless, it still doesn't work
as I wanted. Thanks anyway.



On Sat, Nov 5, 2011 at 3:21 PM, Ishbir Singh <webmaster@ishbir.com> wrote:

> Hey,
>
> In your second route declaration, you've put <function> whereas it should
> be <func>
>
>
> On 5 November 2011 10:00, Jandos Khalik <jandos.kh@gmail.com> wrote:
>
>> I should also say that I'm setting g.lang in
>> url preprocessor like in the documentation:
>>
>> @app.url_value_preprocessor
>> def pull_lang(endpoint, values):
>>         if values:
>>             g.lang = values.pop('lang', None)
>>         else:
>>             g.lang = 'en'
>>
>>
>>
>> On Sat, Nov 5, 2011 at 1:17 PM, Jandos Khalik <jandos.kh@gmail.com>wrote:
>>
>>> If I browse to http://127.0.0.1:5000/jp it says:
>>> AttributeError: 'module' object has no attribute 'jp'
>>> With trailing slash the error will be:
>>>  TypeError: url_handler() got an unexpected keyword argument 'function'
>>>
>>> I think my intention is clear from decorators. But only '/' or full url
>>> including
>>> lang and func works. The other 2 have problem because when only lang is
>>> passed the code assumes it as func etc.
>>>
>>>
>>>
>>> On Sat, Nov 5, 2011 at 12:50 PM, Ishbir Singh <webmaster@ishbir.com>wrote:
>>>
>>>> Hey,
>>>>
>>>> What do you mean by "doesn't work as expected"? What error do you get?
>>>>
>>>>
>>>> On 5 November 2011 06:46, Jandos Khalik <jandos.kh@gmail.com> wrote:
>>>>
>>>>> The following doesn't work as I expect:
>>>>>
>>>>> @app.route('/', defaults={'lang': 'en', 'func': 'index'})
>>>>> @app.route('/<lang>/', defaults={'function': 'index'})
>>>>> @app.route('/<func>', defaults={'lang': 'en'})
>>>>> @app.route('/<lang>/<func>')
>>>>> def url_handler(func):
>>>>>         print func, g.lang
>>>>>         return getattr(views, func)()
>>>>>
>>>>> Can someone explain me what is the proper way?
>>>>> Thank you.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Regards,
>>>> Ishbir Singh
>>>>
>>>>
>>>
>>
>
>
> --
> Regards,
> Ishbir Singh
>
>