[英]How Do I Pass A Parameter into A Get Request to A Flask Blueprint Python App?
I have a flask/blueprint app that works with hard-coded or all()
GET requests.我有一个烧瓶/蓝图应用程序,可以处理硬编码或all()
GET 请求。 This is the code in api.py
:这是api.py
中的代码:
from flask import Blueprint
from ..extensions import db
from ..models.users import Users
api = Blueprint('api', __name__)
@api.route('/user/<name>')
def get_user(email):
users = Users.query.first()
return {'users': users.email}
The above code works and doing GET http://127.0.0.1:5000/user/anythingHere
in Postman returns:上面的代码有效并且在 Postman 中执行GET http://127.0.0.1:5000/user/anythingHere
返回:
{
"users": "test@example.com"
}
Which is correct and corresponds with my database.这是正确的并且与我的数据库相对应。 But when I modify the code in api.py
to look like this:但是当我将api.py
中的代码修改为如下所示时:
from flask import Blueprint
from ..extensions import db
from ..models.users import Users
api = Blueprint('api', __name__)
@api.route('/user/<name>')
def get_user(email):
users = Users.query.filter_by(email='<name>').first()
return {'users': users.email}
And in Postman run http://127.0.0.1:5000/user/test@example.com
hoping that test@example.com
will populate <name>
in filter_by(email='<name>')
, but obviously this doesn't work as indicated by the fact that I get TypeError: get_user() got an unexpected keyword argument 'name'
in my terminal and this in Postman:在 Postman 中运行http://127.0.0.1:5000/user/test@example.com
希望test@example.com
将<name>
填充到filter_by(email='<name>')
中,但显然这不会我得到TypeError: get_user() got an unexpected keyword argument 'name'
in my terminal and this in Postman:
<!doctype html>
<html lang=en>
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or
there is an error in the application.</p>
So how do I pass the email parameter from the GET request to the get_user function?那么如何将 GET 请求中的 email 参数传递给 get_user 函数呢?
You need to pass email
into the function as email
not <email>
like this:您需要将email
作为email
而不是<email>
传递给函数,如下所示:
from flask import Blueprint
from ..extensions import db
from ..models.users import Users
api = Blueprint('api', __name__)
@api.route('/user/<email>')
def get_user(email):
users = Users.query.filter_by(email=email).first()
return {'users': users.email}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.