librelist archives

« back to archive

Static URL building

Static URL building

From:
Ivan Kleshnin
Date:
2013-06-06 @ 09:00
I wonder why flask allows
    url_for('static', filename='css/style.css')
    {{ url_for('static', filename='css/style.css') }}

but cries at
    url_for('static') + '/css/style.css'
    {{ url_for('static') }}/css/style.css

which is shorter and cleaner, especially in HTML version.

There is a way to change this behavior with:
app.url_map.add(werkzeug.routing.Rule('/static', endpoint='static',
build_only=True))

but I'm still interesting why flask authors went with first option.
Maybe I've missed something special?!

Re: [flask] Static URL building

From:
Freedom Dumlao
Date:
2013-06-06 @ 11:51
Shorter yes, but cleaner is highly subjective :)

The job of url_for is to provide a safe, consistent, and future-proof way
of getting urls for our views. Remember that url_for isn't just for static
files, but it's most common usage is in getting urls to your views.

Here are a few points in defense of url_for

1. url_for will return a valid url for a view only in the case where the
url can be found. It's not simply generating something it's actually
looking up to find the right view for the right route. If no view can be
found, you'll get an exception which will help ensure you don't have broken
urls in your production application.

2. Typed and named parameters matter. url_for will distinguish between 2
similar routes with different typed or named parameters.

3. String concatenation isn't safe, and adds mental overhead. In your
example you're suggesting:

    url_for('static') + '/css/style.css'

which would likely (if it worked) produce:

    /static//css/style.css

Now when using url_for, you'd have to remember wether or not you needed to
include a slash prefix in your
concatenated strings - and if you forgot there would be no exception fired
because flask wouldn't know about it.

There are several other reasons that come to mind but I don't want to be
one of "those" guys that goes on and on :)

That being said, it would be fairly trivial to build a filter or template
tag with the behavior you want. Personally I use Flask-Assets so don't end
up using url_for when dealing with my static files.

- Freedom





On Thu, Jun 6, 2013 at 5:00 AM, Ivan Kleshnin <ivan.kleshnin@gmail.com>wrote:

> I wonder why flask allows
>     url_for('static', filename='css/style.css')
>     {{ url_for('static', filename='css/style.css') }}
>
> but cries at
>     url_for('static') + '/css/style.css'
>     {{ url_for('static') }}/css/style.css
>
> which is shorter and cleaner, especially in HTML version.
>
> There is a way to change this behavior with:
> app.url_map.add(werkzeug.routing.Rule('/static', endpoint='static',
> build_only=True))
>
> but I'm still interesting why flask authors went with first option.
> Maybe I've missed something special?!
>

Re: [flask] Static URL building

From:
Ivan Kleshnin
Date:
2013-06-06 @ 13:29
2013/6/6 Freedom Dumlao <freedomdumlao@gmail.com>

> Shorter yes, but cleaner is highly subjective :)
>
> The job of url_for is to provide a safe, consistent, and future-proof way
> of getting urls for our views. Remember that url_for isn't just for static
> files, but it's most common usage is in getting urls to your views.
>
> Here are a few points in defense of url_for
>
> 1. url_for will return a valid url for a view only in the case where the
> url can be found. It's not simply generating something it's actually
> looking up to find the right view for the right route. If no view can be
> found, you'll get an exception which will help ensure you don't have broken
> urls in your production application.
>
> 2. Typed and named parameters matter. url_for will distinguish between 2
> similar routes with different typed or named parameters.
>
> 3. String concatenation isn't safe, and adds mental overhead. In your
> example you're suggesting:
>
>     url_for('static') + '/css/style.css'
>
> which would likely (if it worked) produce:
>
>     /static//css/style.css
>
> Now when using url_for, you'd have to remember wether or not you needed to
> include a slash prefix in your
> concatenated strings - and if you forgot there would be no exception fired
> because flask wouldn't know about it.
>
> There are several other reasons that come to mind but I don't want to be
> one of "those" guys that goes on and on :)
>
> That being said, it would be fairly trivial to build a filter or template
> tag with the behavior you want. Personally I use Flask-Assets so don't end
> up using url_for when dealing with my static files.
>
> - Freedom
>
>
>
>
>
> On Thu, Jun 6, 2013 at 5:00 AM, Ivan Kleshnin <ivan.kleshnin@gmail.com>wrote:
>
>> I wonder why flask allows
>>     url_for('static', filename='css/style.css')
>>     {{ url_for('static', filename='css/style.css') }}
>>
>> but cries at
>>     url_for('static') + '/css/style.css'
>>     {{ url_for('static') }}/css/style.css
>>
>> which is shorter and cleaner, especially in HTML version.
>>
>> There is a way to change this behavior with:
>> app.url_map.add(werkzeug.routing.Rule('/static', endpoint='static',
>> build_only=True))
>>
>> but I'm still interesting why flask authors went with first option.
>> Maybe I've missed something special?!
>>
>
>
I see. Thank you very much.

Re: [flask] Static URL building

From:
Igor Davydenko
Date:
2013-06-06 @ 09:06
If you need only printing static url, why not:

app.py

    app = Flask(__name__)
    app.config.from_object(settings)
    app.config['STATIC_URL'] = app.static_url_path

template.html

    {{ config.STATIC_URL }}/css/style.css

?

For what reason you need to call function each time when you need just a
constant?


On 6 June 2013 12:00, Ivan Kleshnin <ivan.kleshnin@gmail.com> wrote:

> I wonder why flask allows
>     url_for('static', filename='css/style.css')
>     {{ url_for('static', filename='css/style.css') }}
>
> but cries at
>     url_for('static') + '/css/style.css'
>     {{ url_for('static') }}/css/style.css
>
> which is shorter and cleaner, especially in HTML version.
>
> There is a way to change this behavior with:
> app.url_map.add(werkzeug.routing.Rule('/static', endpoint='static',
> build_only=True))
>
> but I'm still interesting why flask authors went with first option.
> Maybe I've missed something special?!
>



-- 
Sincerely,
Igor Davydenko

Re: [flask] Static URL building

From:
Ivan Kleshnin
Date:
2013-06-06 @ 10:11
2013/6/6 Igor Davydenko <playpauseandstop@gmail.com>

> If you need only printing static url, why not:
>
> app.py
>
>     app = Flask(__name__)
>     app.config.from_object(settings)
>     app.config['STATIC_URL'] = app.static_url_path
>
> template.html
>
>     {{ config.STATIC_URL }}/css/style.css
>
> ?
>
> For what reason you need to call function each time when you need just a
> constant?
>
>
> On 6 June 2013 12:00, Ivan Kleshnin <ivan.kleshnin@gmail.com> wrote:
>
>> I wonder why flask allows
>>     url_for('static', filename='css/style.css')
>>     {{ url_for('static', filename='css/style.css') }}
>>
>> but cries at
>>     url_for('static') + '/css/style.css'
>>     {{ url_for('static') }}/css/style.css
>>
>> which is shorter and cleaner, especially in HTML version.
>>
>> There is a way to change this behavior with:
>> app.url_map.add(werkzeug.routing.Rule('/static', endpoint='static',
>> build_only=True))
>>
>> but I'm still interesting why flask authors went with first option.
>> Maybe I've missed something special?!
>>
>
>
>
> --
> Sincerely,
> Igor Davydenko
>

Your are right, I can use constant.
The question was, though, what benefits gives official method over manual
concatenation?