0

I have searched everything I can find on SO including this, flask blueprint template folder.

/root
 __init__.py
 run.py
    /hrms
      __init__.py
      views.py
      models.py
    /static
    /templates
        __init__.py
        layout.html
        /hr
            __init__.py
            hr_template_test.html

I have also tried:

/hrms
  __init__.py
  views.py
  models.py
  /templates
      __init__.py
      hr_template_test.html

No matter where I put the hr_template_test.html file, Flask will not find it. Here is what I have in __init__.py:

from flask import Blueprint, render_template

hrms_blue = Blueprint('hrms', __name__, template_folder='templates')

@hrms_blue.route('/')
def index():
    return render_template('hrms_data_view.html')

In run.py:

from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
from application.views.hrms import hrms_blue

app = Flask(__name__)
app.register_blueprint(hrms_blue, url_prefix='/<hrms_id>')

I have tried:

return render_template('hr/hrms_data_view.html')
return render_template('application/templates/hr/hrms_data_view.html')
return render_template('templates/hr/hrms_data_view.html')
return render_template('hrms_data_view.html')

http://127.0.0.1/hrms

And Jinja2 complains it cannot find the html file.

I took out the __init__.py from templates and hr. No I get, TypeError: index() takes no arguments (1 given).

Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259
  • Your template folder should not have an `__init__.py` file in it. It is not a package. – Martijn Pieters Mar 24 '15 at 18:13
  • I have tried it without it. I will try again. – johnny Mar 24 '15 at 18:13
  • @MartijnPieters I took it out and got TypeError: index() takes no arguments (1 given). I've done this a billion times so I probably forgot I took it out and put it back. Anyway, I get the new error. (I have looked at your many answers here, still reading them.) – johnny Mar 24 '15 at 18:15
  • Your blueprint takes a route parameter but your view is not accepting it. – Martijn Pieters Mar 24 '15 at 18:23
  • @MartijnPieters Please tell me what that means here. – johnny Mar 24 '15 at 18:26
  • You gave the blueprint a URL prefix: `url_prefix='/'`. That's added in front of *any* route you register, so the `index()` view has the route `//`. Your `index()` view now needs to accept a `hrms_id` parameter. – Martijn Pieters Mar 24 '15 at 18:28
  • If I put in this way, @hrms_blue.route('/'), I get you can't use hrms_id twice. I think I need to start over. – johnny Mar 24 '15 at 18:33

1 Answers1

0

You might be having a issue with circular importing. You should try doing:

def register(app):
    from __init__ import hrms_blue
    app.register_blueprint(hrms_blue)

register(app)

instead of doing:

app.register_blueprint(hrms_blue, url_prefix='/<hrms_id>')
Joseph Seung Jae Dollar
  • 1,016
  • 4
  • 13
  • 28