SQLAlchemy出现“ NoneType错误”错误:

时间:2018-11-26 12:46:30

标签: python flask flask-sqlalchemy

我是Flask的初学者。我已经创建了由 models.py 定义的 sqlite数据库,并且已经用值填充了它。我想使用search_var.html文件打印后端数据库表中的所有内容: line_info 。但是,出现错误,请帮忙

我的 models.py

from app  import db
class line_info(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    EUSOL_id = db.Column(db.VARCHAR (255), nullable=True)
    acc_id = db.Column(db.VARCHAR (255), nullable=True)
    acc_name=db.Column(db.String(20), nullable=True)
    reasons = db.Column(db.String(100), nullable=True)
    genbank = db.Column(db.String(100), nullable=True)

    def __init__(self, EUSOL_id,acc_id,acc_name,reasons,genbank):
        self.EUSOL_id= EUSOL_id
        self.acc_id = acc_id
        self.acc_name = acc_name
        self.reasons = reasons
        self.genbank = genbank

我从Python shell中添加了一些虚拟值进行测试:

from app import db, app
from app.models import line_info

l = line_info(EUSOL_id='abcdeg', acc_id='asgd', acc_name='tomato_name', reasons='wild', genbank='NA')
db.app = app
db.init_app(app)
db.create_all()
db.session.add(l)
db.session.commit() 
line_info.query.all()
[<line_info 1>]

我的 routes.py

from flask import render_template
from app import app

@app.route('/')
def search_func():
    line_info = None
    line_info = line_info.query.all()
    return render_template("search_var.html", line_info=line_info)

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

我的“ search_var.html”

{% extends "layout.html" %}
{% block title %}Search{% endblock %}
{% block main %}

   <h1>Lines</h1>
    {% for line in line_info %}
      <p>{{line_info.id}}</p>
    {% endfor %}

{% endfor %}

错误:

Traceback (most recent call last):
  File "/home/workstation1/flask_beta/my_venv/lib/python3.6/site-packages     /flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/workstation1/flask_beta/my_venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/workstation1/flask_beta/my_venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/workstation1/flask_beta/my_venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/workstation1/flask_beta/my_venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/workstation1/flask_beta/my_venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/workstation1/flask_beta/app/routes.py", line 9, in search_func
    line_info = line_info.query.all()
AttributeError: 'NoneType' object has no attribute 'query'

我的目录树

/flask_beta
|__/app
|     |__models.py
|     |__routes.py
|     |__/templates
|                |__search_var.html
|__app.db
|__run.py
|__config.py

2 个答案:

答案 0 :(得分:0)

这是因为用None初始化line_info而不是将其从模型文件导入。

我希望以下代码可以工作。

from flask import render_template
from app import app 

from models import Line_info

@app.route('/')
def search_func():
    line_info = Line_info.query.all()
    return render_template("search_var.html", line_info=line_info)

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

答案 1 :(得分:0)

您无法命名模型line_info,然后将相同的名称用于实例列表。 line_info现在是局部变量,也不能是全局名称,这就是为什么删除UnboundLocalError行时会出现line_info = None异常的原因。

line_info.query.all()结果使用不同的名称。您无需先为None设置任何内容:

from flask import render_template
from app import app
from model import line_info

@app.route('/')
def search_func():
    lines = Line_info.query.all()
    return render_template("search_var.html", line_info=lines)

您仍然可以在模板中使用line_info,尽管实际上也不建议这样做。

相反,我建议您重命名模型以遵循Python naming conventions;使用CamelCase并使用大写字母首字母,不要在_下划线:

class LineInfo(db.Model):
    # ...

现在,与局部变量区分开来要容易得多。