pallets / flask

The Python micro framework for building web applications.
https://flask.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
67.59k stars 16.15k forks source link

Blueprint template not found #1190

Closed serkandaglioglu closed 9 years ago

serkandaglioglu commented 9 years ago

I am trying to do that example http://flask.pocoo.org/docs/0.10/blueprints/#templates .

My application structure

/app
    /modules
        /module_test
            controllers.py
            /templates
                 /test
                      index.html
    /templates

/app/modules/module_test/controllers.py

module = Blueprint('test', "test",  url_prefix='/test', template_folder='templates' )

class TestController(PublicController, View):
    methods = ["GET", "POST"]
    viewData = {}

    def __init__(self):
        super(TestController, self).__init__()

    def dispatch_request(self):
        self.viewData["title"] = "Test"
        return render_template( "test/index.html", **self.viewData )

module.add_url_rule('/', view_func=TestController.as_view('test'))

the error is "jinja2.exceptions.TemplateNotFound TemplateNotFound: test/index.html". i put index.html in /app/templates/test/index.html and i tried with flask classy it worked.

i want to put template files in blueprint that "/app/modules/module_test/templates". How can i solve this problem?

serkandaglioglu commented 9 years ago

I did this change

from

test = Blueprint('test', "test", template_folder='templates' )

to

test = Blueprint('test', "test", template_folder='app/modules/module_test/templates' )

it worked.

But Flask documentation says that:

admin = Blueprint('admin', __name__, template_folder='templates')

As for static files, the path can be absolute or relative to the blueprint resource folder. The template folder is added to the searchpath of templates but with a lower priority than the actual application’s template folder. That way you can easily override templates that a blueprint provides in the actual application.

So if you have a blueprint in the folder yourapplication/admin and you want to render the template 'admin/index.html' and you have provided templates as a template_folder you will have to create a file like this: yourapplication/admin/templates/admin/index.html.

untitaker commented 9 years ago

You should pass __name__ as second argument to Blueprint.

serkandaglioglu commented 9 years ago

@untitaker thanks it worked

untitaker commented 9 years ago

Glad to hear that.