简体   繁体   English

如何修复“”AttributeError: 'NoneType' 对象没有属性 'app'

[英]How to fix ""AttributeError: 'NoneType' object has no attribute 'app'

I am new to web framework and I am trying to do a simple project.我是 Web 框架的新手,我正在尝试做一个简单的项目。 I want to display MySQL data on an html page however I keep getting this AttributeError: 'NoneType' object has no attribute 'app'.我想在 html 页面上显示 MySQL 数据,但是我一直收到这个 AttributeError: 'NoneType' object has no attribute 'app'。

I have looked at other solutions but I can't seem to figure out what I am doing wrong.我查看了其他解决方案,但似乎无法弄清楚我做错了什么。 Any help would be appreciated.任何帮助,将不胜感激。 I did run the this code:我确实运行了这段代码:

TEST.py测试文件

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

print(hello_world())

and this ran fine.这运行良好。

python code蟒蛇代码

import mysql.connector
from flask import Flask, render_template
app = Flask(__name__)

hostname = ''
username = ''
password = ''
database = ''

#run query on a database
@app.route('/')
def doQuery(conn):
    cur = conn.cursor()
    query = "SELECT * FROM maxes"
    cur.execute(query)

    data = cur.fetchall()

    return render_template("weightlifting.html", data=data)

print("Using mysql.connector")
myConnection = mysql.connector.connect(host=hostname, user=username, password=password, db=database)
doQuery(myConnection)
myConnection.close()

This is the error that I am getting:这是我得到的错误:

Using mysql.connector
Traceback (most recent call last):
  File "database.py", line 24, in <module>
    doQuery(myConnection)
  File "database.py", line 20, in doQuery
    return render_template("weightlifting.html", data=data)
  File "C:\Users\kbb_n\Anaconda3_2\lib\site-packages\flask\templating.py", line 133, in render_template
    ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'

In general, you aren't really supposed to directly call the route functions.一般来说,你真的不应该直接调用路由函数。

Your hello_world test just happened to work because that view returns a simple string.您的hello_world测试刚好工作,因为该视图返回一个简单的字符串。

But the doQuery view returns a template render, which is more complicated and requires an actual web request context, which is missing when you just call the function directly.但是doQuery视图返回一个模板渲染,它更复杂,并且需要一个实际的 web 请求上下文,当您直接调用函数时缺少该上下文。

I was getting a similar error when trying to run Python code on Google's AppEngine.尝试在 Google 的 AppEngine 上运行 Python 代码时遇到了类似的错误。 The solution was using app.config.update() to specify the server.解决方案是使用app.config.update()来指定服务器。 I used SERVER_NAME to do this:我使用SERVER_NAME来做到这一点:

app.config.update(
    SERVER_NAME = "127.0.0.1:8080"
)

暂无
暂无

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

相关问题 如何解决“ AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;”的问题 - How to fix “AttributeError: 'NoneType' object has no attribute 'text'” for <span> 如何修复&#39;AttributeError:&#39;NoneType&#39;对象在python中没有属性&#39;get&#39;错误 - How to fix 'AttributeError: 'NoneType' object has no attribute 'get' error in python 如何修复 AttributeError:“NoneType”object 使用 smtplib 没有属性“encode” - How to fix AttributeError: "NoneType" object has no attribute "encode" using smtplib 我该如何解决这个问题:AttributeError: 'NoneType' object has no attribute 'strip - How can i fix this :AttributeError: 'NoneType' object has no attribute 'strip 如何修复 AttributeError:'NoneType' object 没有属性 'loc'(Pandas)? - How to fix AttributeError: 'NoneType' object has no attribute 'loc'(Pandas)? 如何修复 AttributeError: 'NoneType' object 没有属性 'lower'? - How do i fix AttributeError: 'NoneType' object has no attribute 'lower'? “如何解决&#39;AttributeError:&#39;NoneType&#39;对象在Python中没有属性&#39;tbody&#39;错误? - "How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python? 如何修复 AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? - how to fix AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? 我该如何解决这个 AttributeError: 'NoneType' object has no attribute 'text'? - How do i fix this AttributeError: 'NoneType' object has no attribute 'text'? 如何修复 AttributeError: 'NoneType' object 在 insta bot 中没有属性 'click' - How to fix AttributeError: 'NoneType' object has no attribute 'click' in insta bot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM