librelist archives

« back to archive

Bug(?) with "/static" mapping when using a Module with root_path "/"

Bug(?) with "/static" mapping when using a Module with root_path "/"

From:
Osvaldo Santana
Date:
2010-10-15 @ 22:56
Hi Guys,

I'm making some experiments with Flask and I believe that I found a
"strange behaviour" on automatic "/static" url mapping when I register
a Module with "/" root_path.

Take the following application structure:

application/
  __init__.py
  website/
     __init__.py
     views.py
     static/
         favicon.ico
     templates/
         base.html
         index.html


# application/__init__.py
# -----------------------

from flask import Flask

from application.website import module as website_module

def create_app(envvar="PROJECT_SETTINGS"):
   app = Flask(__name__)
   app.register_module(website_module)
   return app


# application/website/__init__.py
# -------------------------------

from flask import Module
from .views import register_urls

module = Module(__name__)
register_urls(module)


# application/website/views.py
# ----------------------------

from flask import render_template

def index():
   return render_template("website/index.html")

def register_urls(module):
   module.add_url_rule("/", "index", index)

[end of code]--------------------------


Everything works fine except that I get a 404 trying to access
/static/favicon.ico.

I'm using a workaround: explicitly setting different static_paths for
Flask and Module. An alternative workaround consists in move
application/website/static directory to application/.

After some investigation I discovered that there is a
self.add_url_rule("/static") in Flask.__init__() and this route
matches the request before the Module's one.

I've some suggestion to "fix" this problem but I don't know what is
the best alternative:

1. Apply the following patch (and document it correctly):
http://paste.pocoo.org/show/276045/
2. Make the described behaviour clear in Flask's documentation.

Thanks,
Osvaldo

-- 
Osvaldo Santana Neto — Triveos Tecnologia
Phone: +55 41 9244-1646

Curso de Desenvolvimento Web com Python e Django — R$200,00 (12xR$19)

Re: [flask] Bug(?) with "/static" mapping when using a Module with root_path "/"

From:
danjac354@gmail.com
Date:
2010-10-16 @ 07:08
Try this:

url_for(".static", filename="favicon.ico")

On 15 October 2010 23:56, Osvaldo Santana <osantana@triveos.com> wrote:
> Hi Guys,
>
> I'm making some experiments with Flask and I believe that I found a
> "strange behaviour" on automatic "/static" url mapping when I register
> a Module with "/" root_path.
>
> Take the following application structure:
>
> application/
>  __init__.py
>  website/
>     __init__.py
>     views.py
>     static/
>         favicon.ico
>     templates/
>         base.html
>         index.html
>
>
> # application/__init__.py
> # -----------------------
>
> from flask import Flask
>
> from application.website import module as website_module
>
> def create_app(envvar="PROJECT_SETTINGS"):
>   app = Flask(__name__)
>   app.register_module(website_module)
>   return app
>
>
> # application/website/__init__.py
> # -------------------------------
>
> from flask import Module
> from .views import register_urls
>
> module = Module(__name__)
> register_urls(module)
>
>
> # application/website/views.py
> # ----------------------------
>
> from flask import render_template
>
> def index():
>   return render_template("website/index.html")
>
> def register_urls(module):
>   module.add_url_rule("/", "index", index)
>
> [end of code]--------------------------
>
>
> Everything works fine except that I get a 404 trying to access
> /static/favicon.ico.
>
> I'm using a workaround: explicitly setting different static_paths for
> Flask and Module. An alternative workaround consists in move
> application/website/static directory to application/.
>
> After some investigation I discovered that there is a
> self.add_url_rule("/static") in Flask.__init__() and this route
> matches the request before the Module's one.
>
> I've some suggestion to "fix" this problem but I don't know what is
> the best alternative:
>
> 1. Apply the following patch (and document it correctly):
> http://paste.pocoo.org/show/276045/
> 2. Make the described behaviour clear in Flask's documentation.
>
> Thanks,
> Osvaldo
>
> --
> Osvaldo Santana Neto — Triveos Tecnologia
> Phone: +55 41 9244-1646
>
> Curso de Desenvolvimento Web com Python e Django — R$200,00 (12xR$19)
>

Re: Bug(?) with "/static" mapping when using a Module with root_path "/"

From:
Osvaldo Santana
Date:
2010-10-16 @ 22:30
Hi,

It won't work because the Module's root_path is registered with "/"
too. So it will generate an url "/static" that is the same url path
already mapped by application (Flask()).

Thanks,
Osvaldo

On 10/16/10, danjac354@gmail.com <danjac354@gmail.com> wrote:
> Try this:
>
> url_for(".static", filename="favicon.ico")
>
> On 15 October 2010 23:56, Osvaldo Santana <osantana@triveos.com> wrote:
>> Hi Guys,
>>
>> I'm making some experiments with Flask and I believe that I found a
>> "strange behaviour" on automatic "/static" url mapping when I register
>> a Module with "/" root_path.
>>
>> Take the following application structure:
>>
>> application/
>>  __init__.py
>>  website/
>>     __init__.py
>>     views.py
>>     static/
>>         favicon.ico
>>     templates/
>>         base.html
>>         index.html
>>
>>
>> # application/__init__.py
>> # -----------------------
>>
>> from flask import Flask
>>
>> from application.website import module as website_module
>>
>> def create_app(envvar="PROJECT_SETTINGS"):
>>   app = Flask(__name__)
>>   app.register_module(website_module)
>>   return app
>>
>>
>> # application/website/__init__.py
>> # -------------------------------
>>
>> from flask import Module
>> from .views import register_urls
>>
>> module = Module(__name__)
>> register_urls(module)
>>
>>
>> # application/website/views.py
>> # ----------------------------
>>
>> from flask import render_template
>>
>> def index():
>>   return render_template("website/index.html")
>>
>> def register_urls(module):
>>   module.add_url_rule("/", "index", index)
>>
>> [end of code]--------------------------
>>
>>
>> Everything works fine except that I get a 404 trying to access
>> /static/favicon.ico.
>>
>> I'm using a workaround: explicitly setting different static_paths for
>> Flask and Module. An alternative workaround consists in move
>> application/website/static directory to application/.
>>
>> After some investigation I discovered that there is a
>> self.add_url_rule("/static") in Flask.__init__() and this route
>> matches the request before the Module's one.
>>
>> I've some suggestion to "fix" this problem but I don't know what is
>> the best alternative:
>>
>> 1. Apply the following patch (and document it correctly):
>> http://paste.pocoo.org/show/276045/
>> 2. Make the described behaviour clear in Flask's documentation.
>>
>> Thanks,
>> Osvaldo
>>
>> --
>> Osvaldo Santana Neto — Triveos Tecnologia
>> Phone: +55 41 9244-1646
>>
>> Curso de Desenvolvimento Web com Python e Django — R$200,00 (12xR$19)
>>
>

-- 
Sent from my mobile device

Osvaldo Santana Neto — Triveos Tecnologia <http://www.triveos.com.br/>
Phone: +55 41 9244-1646

Curso de Desenvolvimento Web com Python e
Django<http://www.ludeos.com.br/s/triveos-cursos-online> —
R$200,00 (12xR$19)