简体   繁体   English

在读取/显示数据时使用烧瓶/python/sqlalchemy 出现内部 500 错误

[英]Getting an Internal 500 error with flask/python/sqlalchemy in reading/displaying data

The code runs fine in flask, until I add the code to return data using sqlalchemy from the database and display it using the corresponding tag in the html section.代码在 flask 中运行良好,直到我添加代码以使用数据库中的 sqlalchemy 返回数据并使用 html 部分中的相应标签显示它。 The error in pycharm is stating that:UnboundLocalError: local variable 'unames' referenced before assignment. pycharm 中的错误说明:UnboundLocalError: local variable 'unames' referenced before assignment。 I'm not sure that I can reference the tags as global?我不确定我可以将标签引用为全局标签吗? Am I missing something obvious?我错过了一些明显的东西吗?

This is a proof of concept to show that data from mysql can be returned and displayed using flask for a small project.这是一个概念证明,表明可以使用 flask 为小型项目返回和显示来自 mysql 的数据。

this is the app.py这是app.py

import os

from flask import Flask
from flask import render_template
from flask import request

from flask_sqlalchemy import SQLAlchemy

project_dir = os.path.dirname(os.path.abspath(__file__))


app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = 'mysql://username:password@localhost:3306/database'

db = SQLAlchemy(app)

class Users(db.Model):
    uname = db.Column(db.String(80), unique=True, nullable=False, primary_key=True)

    def __repr__(self):
        return "<uname: {}>".format(self.uname)


@app.route("/", methods=["GET", "POST"])
def home():
if request.form:
users = Users(uname=request.form.get("uname"))
db.session.add(users)
db.session.commit()
unames = Users.query.all()
return render_template("home.html", unames=unames)

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

This is the home.html code:这是家html代码:

<h1>UserNames:</h1>
    {% for uname in unames %}
      <p>{{Users.uname}}</p>
    {% endfor %}

Expected results should be displaying 2 usernames that are in the uname column from the Users table.预期结果应显示用户表中 unname 列中的 2 个用户名。

There is a mistake in the HTML HTML 有错误

<p>{{Users.uname}}</p>

You are referencing to Users.uname instead it should refer to the loop variable uname.uname您引用的是Users.uname而不是它应该引用循环变量uname.uname

<h1>UserNames:</h1>
    {% for user in unames %}
      <p>{{user.uname}}</p>
    {% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 500 内部服务器错误 Flask 应用程序 - getting 500 Internal server error Flask app 使用 Flask JWT 时出错,AttributeError: &#39;list&#39; 对象没有属性 &#39;id&#39; 并显示 500 内部服务器错误 - getting error while using Flask JWT, AttributeError: 'list' object has no attribute 'id' and shows 500 Internal server error 使用Flask和Jinja时500内部服务器错误 - 500 Internal Server Error When Using Flask and Jinja Flask 返回 500 成功返回后内部错误 - Flask returning 500 Internal error after successful return 为什么通过python请求调用rest api时出现500 Internal Server Error? - Why am I getting 500 Internal Server Error when calling post rest api via python request? 为什么我会收到 500 内部服务器错误消息? - Why am I getting the 500 Internal Server Error message? 在 python 中出现 flask 的 ModuleNotFound 错误 - Getting a ModuleNotFound error with flask in python 烧瓶错误 500 内部服务器错误。 使用电子邮件/密码进行 Firebase 身份验证 - Flask error 500 Internal Server Error. Firebase-authentication with email/password [Python] [Tornado]:在页面之间导航时出现500内部服务器错误 - [Python][Tornado]: 500 Internal Server Error when navigating between pages 为什么我的 python 应用程序继续收到 500 内部服务器错误? - Why is my python app continues to get a 500 Internal Server Error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM