Re: [flask] Per-module template directories
- From:
- Robert Mela
- Date:
- 2010-11-10 @ 21:22
Yes, the Flask module is a Python package. Here's everything needed to
reproduce the problem. Test URL is http://localhost:5000/foo
When I call render_template('hello.html') from inside the module, I get
the app-level hello.html template. If I remove or rename the app-level
hello.html template, then I get an HTTP 500.
./__init__.py ( empty )
./run.py
#!/usr/bin/env python
from flask import Flask
app=Flask(__name__)
from foomodule import foo
app.register_module(foo.foo, url_prefix='/foo')
app.run()
./templates/hello.html - document that says "Hello from App"
./foomodule
./foomodule/__init__.py ( empty )
./foomodule/foo.py
from flask import Module, render_template
foo=Module(__name__, url_prefix='/foo' )
@foo.route('/')
def index():
return render_template('hello.html')
./foomodule/templates/hello.html - a document that says "Hello from
Module Foo"
On 11/10/10 11:10 AM, Dag Odenhall wrote:
> On Wed, 2010-10-27 at 18:02 -0400, Robert Mela wrote:
>> Is there a standard way to have template directories specific to modules?
>>
>> I have a module, and when Flask initializes the module, it gives the
>> module object a jinja_loader.
>>
>> The jinja_loader has a searchpath list, and the list contains a path to
>> the correct template directory in my module's directory.
>>
>> However, flask.render_template ignores the module's jinja_loader and
>> uses the application's jinja_loader instead, with the result that only
>> the application template directory is searched.
>>
>> It would be nice if maybe I could pass a reference to my module's
>> jinja_loader to the render_template method, so I could override the
>> application search path with my module's search path.
> Is your Flask module a Python package?
>
> mod/__init__.py
> mod/templates/
>
>
Re: [flask] Per-module template directories
- From:
- Dag Odenhall
- Date:
- 2010-11-10 @ 21:36
On Wed, 2010-11-10 at 16:22 -0500, Robert Mela wrote:
> Yes, the Flask module is a Python package. Here's everything needed to
> reproduce the problem. Test URL is http://localhost:5000/foo
>
> When I call render_template('hello.html') from inside the module, I get
> the app-level hello.html template. If I remove or rename the app-level
> hello.html template, then I get an HTTP 500.
>
> ./__init__.py ( empty )
> ./run.py
> #!/usr/bin/env python
> from flask import Flask
> app=Flask(__name__)
> from foomodule import foo
> app.register_module(foo.foo, url_prefix='/foo')
> app.run()
> ./templates/hello.html - document that says "Hello from App"
>
> ./foomodule
> ./foomodule/__init__.py ( empty )
> ./foomodule/foo.py
> from flask import Module, render_template
> foo=Module(__name__, url_prefix='/foo' )
> @foo.route('/')
> def index():
> return render_template('hello.html')
> ./foomodule/templates/hello.html - a document that says "Hello from
> Module Foo"
You want "foo" in __init__.py because foo.py is a module in a package,
not a package itself.
Solution ( Re: [flask] Per-module template directories )
- From:
- Robert Mela
- Date:
- 2010-11-10 @ 21:55
Found it ... based on the tutorial section at
http://flask.pocoo.org/docs/patterns/packages/#modules-and-resources
The layout as shown below does in fact work ....
What I needed to do was qualify the template path with the module name
in render_template --
In other words, in the code I provided below, changing
"render_template('hello.html')
to
"render_template('foo/hello.html')"
causes the correct template to be rendered.
Same is true for the layout you suggest -- moving foo into __init__.py
makes no difference. The key is that the variable name of the Module
needs to be the prefix path to the template file ( and that's the
flask.Module instance, not module ).
did the trick. Though I find it a bit confusing that the
On 11/10/10 4:36 PM, Dag Odenhall wrote:
> On Wed, 2010-11-10 at 16:22 -0500, Robert Mela wrote:
>> Yes, the Flask module is a Python package. Here's everything needed to
>> reproduce the problem. Test URL is http://localhost:5000/foo
>>
>> When I call render_template('hello.html') from inside the module, I get
>> the app-level hello.html template. If I remove or rename the app-level
>> hello.html template, then I get an HTTP 500.
>>
>> ./__init__.py ( empty )
>> ./run.py
>> #!/usr/bin/env python
>> from flask import Flask
>> app=Flask(__name__)
>> from foomodule import foo
>> app.register_module(foo.foo, url_prefix='/foo')
>> app.run()
>> ./templates/hello.html - document that says "Hello from App"
>>
>> ./foomodule
>> ./foomodule/__init__.py ( empty )
>> ./foomodule/foo.py
>> from flask import Module, render_template
>> foo=Module(__name__, url_prefix='/foo' )
>> @foo.route('/')
>> def index():
>> return render_template('hello.html')
>> ./foomodule/templates/hello.html - a document that says "Hello from
>> Module Foo"
> You want "foo" in __init__.py because foo.py is a module in a package,
> not a package itself.
>
>
>