简体   繁体   English

Flask AttributeError: 'NoneType' object 没有属性 'name'

[英]Flask AttributeError: 'NoneType' object has no attribute 'name'

Working on a simple web app that should return "Hello, (name pulled from database)."处理一个简单的 web 应用程序,该应用程序应该返回“你好,(从数据库中提取的名称)”。 It's saying that the object has no attribute for 'name' even though name is listed as an attribute in the class.据说the object has no attribute for 'name'即使名称在 class 中列为属性。 Can't figure out why it thinks it's not there (unless I'm misunderstanding the error)?无法弄清楚为什么它认为它不存在(除非我误解了错误)?

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app=Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres@localhost:5432/example'
db = SQLAlchemy(app)

# initalizes the class/table
class Person(db.Model):
    __tablename__ = 'persons'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)

db.create_all()

@app.route('/')
def index():
    person = Person.query.first()
    return 'Hello ' + person.name

Error:错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/ZEMEL/Dropbox (Personal)/Code/hello/flask-hello-app.py", line 20, in index
    return 'Hello ' + person.name
AttributeError: 'NoneType' object has no attribute 'name'

your db either has no content in it or you are fetching the data the wrong way.您的数据库中没有内容,或者您以错误的方式获取数据。

person=Person.query(name="name").first() 

should fetch the data with the first person with the name being "name"应该用名字为“name”的第一个人来获取数据

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

相关问题 AttributeError: 'NoneType' 对象没有属性 'shape' FLASK - AttributeError: 'NoneType' object has no attribute 'shape' FLASK Flask AttributeError:'NoneType'对象没有属性'request' - Flask AttributeError: 'NoneType' object has no attribute 'request' Flask AttributeError:“ NoneType”对象没有属性“ split” - Flask AttributeError: 'NoneType' object has no attribute 'split' AttributeError: 'NoneType' 对象没有属性 'name'? - AttributeError: 'NoneType' object has no attribute 'name'? AttributeError:'NoneType'对象没有属性'name - AttributeError: 'NoneType' object has no attribute 'name AttributeError:'NoneType'对象没有属性'name' - AttributeError: 'NoneType' object has no attribute 'name' AttributeError: 'NoneType' object 没有带有 streamlit 的属性 'name' - AttributeError: 'NoneType' object has no attribute 'name' with streamlit 烧瓶错误:AttributeError:'NoneType'对象没有属性'get' - Flask error : AttributeError: 'NoneType' object has no attribute 'get' Flask SQLAlchemy AttributeError:'NoneType'对象没有属性'append' - Flask SQLAlchemy AttributeError: 'NoneType' object has no attribute 'append' AttributeError: 'NoneType' 对象没有 Flask Web App 的属性 'drivername' - AttributeError: 'NoneType' object has no attribute 'drivername' for Flask Web App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM