Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t通过flask将变量传递给html_Python_Postgresql_Flask_Sqlalchemy - Fatal编程技术网

Python Can';t通过flask将变量传递给html

Python Can';t通过flask将变量传递给html,python,postgresql,flask,sqlalchemy,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 insta

我试图通过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
类型.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>
...
但当我运行它时,我得到“TypeError:‘function’object不可编辑”

我做错了什么

回溯更新

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:module'types'没有属性'all_types'

这里有两个东西叫做
all_types
,一个是你的处理程序,另一个是你的实用程序函数,这很容易混淆。但事实上你并没有给他们中的任何一个打电话。您所做的是将对当前处理程序函数的引用传递到模板中,而模板自然不知道如何处理它

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

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

这里有两个东西叫做
all_type
——你的处理程序和你的实用程序函数——这让人很困惑。但事实上你并没有给他们中的任何一个打电话。您所做的是将对当前处理程序函数的引用传递到模板中,而模板自然不知道如何处理它

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

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

请包括完整的回溯。{%对于类型%s中的类型,此获取类型错误
all_types
是函数的名称(与您尝试在其中使用它的名称相同)。我不确定您希望它是什么,但您可能希望给它一个不同的名称。这不是回溯;我要求提供错误之前的所有行。请注意,这是一个非常非常规的应用程序设置-
config.py
创建db连接?明白了。使用Daniel Roseman suggesti的回溯更新了它ONS请包含完整的回溯。{%对于类型%s中的类型,此获取类型错误
all_types
是一个函数的名称(与您尝试在其中使用它的名称相同)。我不确定您希望它是什么,但您可能希望给它一个不同的名称。这不是回溯;我要求提供错误之前的所有行。请注意,这是一个非常非常规的应用程序设置-
config.py
创建db连接?明白了。使用Daniel Roseman suggesti的回溯更新了它谢谢,现在我得到了:返回render_模板(“index.html”,types=types.all_types())AttributeError:module'types'没有属性'all_types',你的types.py文件相对于你的app.py在哪里?看起来Python正在导入内置的
类型
模块。无论如何,你应该把你的文件称为其他东西。谢谢你!你教了我一个新的编码维度。我正在修复本地和全局函数。Thanks,现在我得到了:返回render_模板(“index.html”,types=types.all_types())AttributeError:module'types'没有属性'all_types',你的types.py文件相对于你的app.py在哪里?看起来Python正在导入内置的
类型
模块。无论如何,你应该把你的文件称为其他东西。谢谢你!你教了我一个新的编码维度。我正在修复本地和全局函数。