无法通过烧瓶将变量传递给html

时间:2019-02-23 18:44:54

标签: python postgresql flask sqlalchemy

我正在尝试通过SQLAlchemy从Postgressql中获取数据,并将项目循环到html页面中。

我做错了,但我无法指出。

config.py

import connexion
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow


connex_app = connexion.App(__name__)

    # Get the underlying Flask app instance
    app = connex_app.app

# Configure the SqlAlchemy part of the app instance
app.config["SQLALCHEMY_ECHO"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://hey:hey2@localhost/heys"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

# Create the SqlAlchemy db instance
db = SQLAlchemy(app)

# Initialize Marshmallow
ma = Marshmallow(app)

models.py

from config import db, ma
from sqlalchemy import Column, Integer, String


class types(db.Model):
    __tablename__='types'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

class TypesSchema(ma.ModelSchema):
    class Meta:
        model = types
        sqla_session = db.session

types.py

from flask import make_response, abort
from config import db
from models import types, TypesSchema

def all_types():

    # Create the list of wine type from our data
    types = types.query.order_by(types.id).all()
    # Serialize the data for the response
    types_schema = TypesSchema(many=True)
    data = types_schema.dump(types).data
    return data

app.py

from flask import render_template
import json
# local modules
import config

# Get the application instance
connex_app = config.connex_app

# create a URL route in our application for "/"
@connex_app.route("/")
def all_types():
    return render_template("index.html", types=all_types)

if __name__ == "__main__":
    connex_app.run(debug=True)

index.html

... 
<tbody>
           {% for type in types %}
              <h1>Name: {{type.name}}</h1>
              <h2>ID: {{type.id}}</h2>
          {% endfor %}
</tbody>
...

types.py的返回值为

[{'id': 1, 'name': 'Red wine'}, {'id': 2, 'name': 'White wine'}, {'id': 3, 'name': 'Sparkling'}, {'id': 4, 'name': 'Rosé'}, {'id': 7, 'name': 'Sweet Wine'}, {'id': 24, 'name': 'Tawny'}, {'id': 25, 'name': 'Not Classified'}]

但是当我运行它时,出现“ TypeError:'function'对象不可迭代”的情况。

我做错了什么?

跟踪更新

File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/2/Library/Python/3.7/lib/python/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/2/Desktop/Python/Vino_app/app.py", line 23, in all_types
return render_template("index.html", types=types.all_types())

AttributeError:模块'types'没有属性'all_types'

1 个答案:

答案 0 :(得分:0)

这里有两个叫做all_types的东西-您的处理程序和您的实用程序函数-令人困惑。但实际上,您实际上都不是在呼叫他们。您正在做的是将对当前处理函数的引用传递到模板中,这自然不知道该如何处理。

您需要将类型模块导入到apps.py中,然后传递调用函数的结果:

import types
...
@connex_app.route("/")
def all_types():
    return render_template("index.html", types=types.all_types())