将Flask代码转换为包并获取TemplotNotFound错误

时间:2018-09-25 16:05:30

标签: python html flask

我正在尝试将Flask代码转换为python包结构,但是现在,当我尝试运行代码时,没有找到HTML模板。

这是我当前烧瓶包装的简化结构。 run.py是我用来执行Flask代码的工具。 routes.py包含我所有的@app.routes HTML模板被调用的地方。

pipeline-ui/
   run.py
   pipeline_app/
       __init__.py
       routes.py
       models.py
       forms.py
       templates/
          pipeline-alt.html

我收到的错误消息是:TemplateNotFound: pipeline-alt.html

init.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager


app = Flask('__name__')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
login_manager.login_message_category = 'info'

from pipeline_app import routes

run.py:

from pipeline_app import app 

if __name__ == '__main__':
    app.run(debug=True)

routes.py:

from flask import url_for, render_template, request, flash, redirect
from pipeline_app import app
from pipeline_app.forms import InputForm, RegistrationForm, LoginForm
from pipeline_app.models import User, Post
from flask_login import login_user, current_user, logout_user, login_required
from math import ceil
import json
import subprocess
import os
import sys
from pprint import pprint

@app.route('/', methods=['GET', 'POST'])
def pipeline(): 
    form = InputForm(request.form)

    if request.method == 'POST':
        if form.validate_on_submit():
            STACK_NAME = request.form['stack_name']

            # Store data in database for future use

            db.create_all()
            db_inputs = Post(stack_name=STACK_NAME)
            db.session.add(db_inputs)
            db.session.commit()

    return render_template('pipeline-alt.html', 
                            title='Pipeline Input', 
                            form=form,
                            STACK_NAME=STACK_NAME)

1 个答案:

答案 0 :(得分:0)

app = Flask('__name__')更改为app = Flask(__name__)。 Flask使用此参数在文件系统上查找资源,因此,如果未正确设置它,将无法找到您的模板。