gpt4 book ai didi

python - 无法通过 flask 将变量传递给html

转载 作者:行者123 更新时间:2023-11-29 13:41:24 26 4
gpt4 key购买 nike

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

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

配置.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)

模型.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

类型.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

应用.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' object is not iterable”。

我做错了什么?

追溯更新

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'

最佳答案

这里有两个叫做 all_types 的东西——你的处理程序和你的实用函数——这很令人困惑。但事实上,您实际上并没有调用它们中的任何一个。您正在做的是将对当前处理程序函数的引用传递到您的模板中,模板自然不知道如何处理它。

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

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

关于python - 无法通过 flask 将变量传递给html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54844891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com