librelist archives

« back to archive

Always use url_for('...', _external=True )?

Always use url_for('...', _external=True )?

From:
chrickso
Date:
2012-10-16 @ 13:10
I do not want to use relative links in my app and am wondering if there is
a way to perminately set the url_for _external setting to true, without my
having to specify every time?

Thanks!

Re: [flask] Always use url_for('...', _external=True )?

From:
Igor Davydenko
Date:
2012-10-16 @ 13:20
Just override default url_for somewhere in your application. It could
seems like,

    from flask import current_app, url_for as base_url_for

    def url_for(url_rule, **kwargs):
        kwargs.setdefault('_external', True)
        return base_url_for(url_rule, **kwargs)

    current_app.jinja_env.globals['url_for'] = url_for

On 16 October 2012 16:10, chrickso <chrickso@gmail.com> wrote:
> I do not want to use relative links in my app and am wondering if there is a
> way to perminately set the url_for _external setting to true, without my
> having to specify every time?
>
> Thanks!



-- 
Sincerely,
Igor Davydenko

Re: [flask] Always use url_for('...', _external=True )?

From:
Simon Sapin
Date:
2012-10-16 @ 13:19
Le 16/10/2012 15:10, chrickso a écrit :
> I do not want to use relative links in my app and am wondering if there
> is a way to perminately set the url_for _external setting to true,
> without my having to specify every time?

Try this:


     from flask import url_for as flask_url_for

     def url_for(endpoint, **kwargs):
         kwargs.setdefault('_external', True)
         return flask_url_for(endpoint, **kwargs)

     app.jinja_env.globals['url_for'] = url_for


The jinja_env line takes care of templates, but in Python code you need 
to make sure you use this new function rather than flask’s.


Cheers,
-- 
Simon Sapin

Re: [flask] Always use url_for('...', _external=True )?

From:
Graeme Coupar
Date:
2012-10-16 @ 13:21
You could just define a url_for proxy function then call that rather than
the normal url_for.  Something like this:

@app.template_filter('url_for')
def url_for( *pargs, external=True, **kwargs ):
    return flask.url_for( *pargs, external, **kwargs )

On Tue, Oct 16, 2012 at 2:10 PM, chrickso <chrickso@gmail.com> wrote:

> I do not want to use relative links in my app and am wondering if there is
> a way to perminately set the url_for _external setting to true, without my
> having to specify every time?
>
> Thanks!
>