属性错误:'function'对象没有属性'route'



Here is my app.py**正如您所看到的,我一直在尝试将add_user((函数重定向到/src/Form/basicForm.js,但我不能。我想我错过了一些东西。**

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy 
from models import User, Admin
POSTGRES = {
'user': 'postgres',
'pw': 'jarvis123@#$',
'db': 'flask_database',
'host': 'localhost',
'port': '5432',
}
app = Flask(__name__)
db=SQLAlchemy(app)
app.config['DEBUG']=True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES
db.init_app(app)
@app.route('/adduser/<int:id>', methods=['GET','POST '])
def add_user():
if request.method == 'POST':
name = request.args.get('name')
email = request.args.get('email')
password_hash = request.args.get('password_hash')
try:
user = User(
name=name,
email=email,
password_hash=password_hash
)
db.session.add(user)
db.session.commit()
# return render_template("../src/Form/basicForm.js")
except exception as e:
return (str(e))
return render("../src/Form/basicForm.js")

Here is the error:**它一直显示与"路线"有关的属性错误**

File "/home/manoj/Desktop/registration-app/api/app.py", line 36, in <module>
@app.route('/adduser/<int:id>', methods=['GET','POST '])
AttributeError: 'function' object has no attribute 'route'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! registration-app@0.1.0 start-flask-api: `cd api && venv/bin/flask run`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the registration-app@0.1.0 start-flask-api script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/manoj/.npm/_logs/2021-04-02T08_55_02_861Z-debug.log```

如果您有一个函数名为app()的路由,就会发生这种情况

像这样:

@app.route('/')
def app():
return "Hello World"

将函数名称更改为:

@app.route('/')
def _app():
return "Hello World"

尝试在app.py文件的末尾添加以下行:

if __name__ == '__main__':
try:
app.run(host='0.0.0.0', port=5050, debug=True)
except Exception as e:
print(str(e))

运行文件app.py

最新更新