我想执行场景,如果具有 user_id 的用户首先在 Sign_up 表中注册,则用户只能申请工作,对于上述查询和路线,我尝试同时申请注册用户和 uu 注册用户的工作,但我得到相同的输出
[
null,
"User not authenticated or signed up"
]
我希望如果用户在sign_up表中注册并申请工作,则应呈现指定的html模板
我用的是这个sql查询功能
def get_user_id_from_request():
"""
Retrieve user_id from the sign_up table based on some identifier in the request.
"""
username = request.headers.get("full_name")
if username:
with engine.connect() as conn:
query = text("SELECT user_id FROM sign_up WHERE username = :username")
result = conn.execute(query, {'username': username}).fetchone()
if result:
return result[0] # Return the user_id if found
return None
和这个 api 端点
"@app.route("/job/<id>/apply", methods=["post"])
def apply_job(id):
"""
get the data from form and insert the data to the
database table by using post method and displays and acknowledgment
"""
data= request.form
job = get_job_by_id(id)
user_id = get_user_id_from_request()
if insert_application(user_id, id, data):
return render_template("app_submitted.html",application=data, job=job)
else:
return jsonify(user_id,"User not authenticated or signed up")"
终于在挣扎了很多时间之后我修复了它,这就是我修复它的方法 使用
request.headers.get("full_name")
并不是在请求标头中包含敏感信息(例如用户名)的常见做法。通常,用户名是通过表单收集的或作为身份验证过程的一部分提供。
所以我们必须使用下面的方法来代替上面的方法
username = request.form.get("username")