[英]Python flask api routes 404 Not found error
As the title of the question says, all the routes which are not in the main file ie app.py
are throwing Not Found 404
error.正如问题的标题所说,所有不在主文件(即
app.py
中)的路由都抛出Not Found 404
错误。 Below are the details of my small project:以下是我的小项目的详细信息:
Directory structure:目录结构:
server
-> user
-> __init__.py
-> models.py
-> routes.py
-> app.py
-> __init__.py
app.py
: app.py
:
from flask import Flask
app = Flask(__name__)
from user import routes
@app.route('/') # THIS ROUTE WORKS
def index():
return '<h1>HOME</h1>'
if __name__ == '__main__':
app.run(debug = True)
routes.py
: routes.py
:
from app import app
@app.route('/register', methods=['GET']) # THIS DOESN'T WORK - 404 NOT FOUND ERROR
def register_user():
return '<h1>Register a user!</h1>'
I cannot find out what is wrong here, I also tried the Blueprint
method but I keep getting circular import error
.我找不到这里出了什么问题,我也尝试了
Blueprint
方法,但我一直收到circular import error
。
EDIT :编辑:
Flask Blueprint approach: Flask 蓝图方法:
app.py:应用程序.py:
from flask import Flask
from pymongo import MongoClient
app = Flask(__name__)
mongo_client = MongoClient(mongoUri)
db = mongo_client[DB_NAME]
from user.routes import bp
app.register_blueprint(bp)
@app.route('/')
def index():
return '<h1>HOME</h1>'
if __name__ == '__main__':
app.run(debug = True)
user/routes.py:用户/routes.py:
from flask import Blueprint, render_template_string
from user.models import User # if I comment this, it starts working...
bp = Blueprint('user', __name__)
@bp.route('/register', methods=['POST'])
def register_user():
user = User()
user.register()
return user
@bp.route('/register', methods=['GET'])
def check_user():
return render_template_string('<h1>User Registered</h1>')
user/models.py:用户/models.py:
import uuid
from urllib import request
from flask import jsonify
from app import db # fishy...
class User(object):
def register(self):
user = {
'_id': uuid.uuid4().hex,
'email': request.form.get('email'),
'password': request.form.get('password'),
}
db.users.insert_one(user):
return jsonify(user), 200
I'm now getting this error as I mentioned earlier:正如我之前提到的,我现在遇到了这个错误:
Traceback (most recent call last):
File "D:\project\server\app.py", line 10, in <module>
from user.routes import bp
File "D:\project\server\user\routes.py", line 2, in <module>
from user.models import User
File "D:\project\server\user\models.py", line 5, in <module>
from app import db
File "D:\project\server\app.py", line 10, in <module>
from user.routes import bp
ImportError: cannot import name 'bp' from partially initialized module 'user.routes' (most likely due to a circular import) (D:\project\server\user\routes.py)
You need to first define a Blueprint, and then use that for your routes/APIs.您需要先定义一个蓝图,然后将其用于您的路由/API。 In
route.py
write this:在
route.py
写:
from flask import Blueprint, render_template_string
from user.models import User
bp = Blueprint('user', __name__)
@bp.route('/register', methods=['POST'])
def register_user():
user = User()
user.register()
return user
@bp.route('/register', methods=['GET'])
def check_user():
return render_template_string('<h1>User Registered</h1>')
Then you need to import and register your blueprint.然后您需要导入并注册您的蓝图。 In app.py have this:
在 app.py 中有这个:
from flask import Flask
app = Flask(__name__)
from user.routes import bp
app.register_blueprint(bp)
@app.route('/')
def index():
return '<h1>HOME</h1>'
if __name__ == '__main__':
app.run(debug = True)
Have another file in the same directory as app.py
, you can call it extensions.py
:在与
app.py
相同的目录中有另一个文件,您可以将其称为extensions.py
:
from pymongo import MongoClient
mongo_client = MongoClient(mongoUri)
db = mongo_client[DB_NAME]```
And finally models.py
will look like this:最后
models.py
将如下所示:
...
from extensions import db
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.