【问题标题】:using another WS as validation Flask/Rest/Mysql使用另一个 WS 作为验证 Flask/Rest/Mysql
【发布时间】:2019-04-19 13:24:07
【问题描述】:

我正在尝试使用 3 个 Web 服务构建一个简单的 Web 应用程序。我的两个网络服务应该验证学生是否存在于课程中。这是通过一个简单的 SELECT 查询来完成的。我的第三个 Web 服务应该将学生添加到数据库中,但前提是该学生确实存在于特定课程中。

这是我的验证 WS,它应该返回真/假。

@app.route('/checkStudOnCourse/<string:AppCode>/<string:ideal>', methods= ["GET"])
def checkStudOnCourseWS(AppCode, ideal):

myCursor3 = mydb.cursor()
query3 = ("SELECT studentID FROM Ideal.course WHERE applicationCode = " + "'" + AppCode + "' AND Ideal = " + "'" + ideal + "'")
myCursor3.execute(query3)
myresult3 = myCursor3.fetchall()

if len(myresult3) == 0:
    return render_template('Invalid.html')
else:
    return jsonify({'Student in course ': True})

下面是 regResult,它应该对数据库执行 SQL 插入。如果上述结果为“真”,我只希望提交工作,我该怎么做?我知道我没有完成 INSERT 查询,但这不是问题。 我不确定的是:如果验证 WS 为“True”,我怎样才能让提交被插入。

@app.route('/register', methods=["POST", "GET"])
def regResultat():


if request.method == "POST":

    Period = request.form['period']
    #ProvNr = request.form['provNr']
    Grade = request.form['grade']
    Applicationcode = request.form['applicationcode']
    #Datum = request.form['datum']
    Ideal = request.form['ideal']

CheckStudOnCourse = 'http://127.0.0.1:5000/checkAppCodeWS/'+Applicationcode+'/'+Ideal
CheckStudOnResp = requests.get(CheckStudOnCourse)

【问题讨论】:

    标签: python mysql rest flask flask-restful


    【解决方案1】:

    一开始是这样的语法:

    if len(myresult3) == 0,可以简化为 if myresult3,因为 Python 将其计算为 bool。

    其次,如果你曾经从函数返回,就不需要写else语句了:

        if len(myresult3) == 0:
            return render_template('Invalid.html')  #  < -- in case 'True',
                                                    #  it returns here, otherwise 
                                                    #  function keeps going"""
    
         return jsonify({'Student in course ': True})  # < -- in case 'False', it is returned here
    

    专注于您的问题,您可以这样做:

    从 ws 中获取价值

    CheckStudOnCourse = 'http://127.0.0.1:5000/checkAppCodeWS/'+Applicationcode+'/'+Ideal
    CheckStudOnResp = requests.get(CheckStudOnCourse)
    

    从中提取json:

    if result_as_json.status == 200:
        result_as_json = CheckStudOnResp.json()  #  < -- it is now a dict
    

    做一些检查:

    if result_as_json.get('Student in course', False):  #  I highly suggest to use other 
                                                       #  convention to name json keys 
                                                       #  e.g. Student in course -> 
                                                       #  student_exists_in_course
        #  do your code here
    

    【讨论】:

    • 感谢您对我的代码的反馈和 cmets。我明白你在说什么以及为什么它更好。当我尝试这样做时,我收到一条错误消息“ValueError: No JSON object could be decoded”“if stud_exists_in_course.get('Student in course', True): myCursor4.execute("INSERT INTO Ladok.resultat (Kurskod, Ideal , betyg, datum, anmalningskod) VALUES (%s, %s, %s, %s, %s)", (Kurskod, Ideal, Betyg, Period, Anmalningskod)) mydb4.commit()"
    • 你能告诉我CheckStudOnResp 包含什么吗?只需print(str(CheckStudOnResp))
    • 响应是否有状态200 OK
    • 我的控制台中有一个 。还有“TypeError:视图函数没有返回有效的响应。该函数要么返回 None,要么在没有返回语句的情况下结束。”我在 IF 语句之前添加了 print(str(checkstudOnResp))。
    • 如果我只运行127.0.0.1:5000/checkStudOnCourse/ltu-123/jonoke-6(这是我的 CheckStudOnCoursewS(Appcode, Ideal),那么我会得到一个有效的回报(“当然学生,TRUE”)这可能更容易在我的 sn- p.snipplr.com/view/329870/flask
    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2014-10-14
    • 2015-04-01
    • 1970-01-01
    • 2020-02-17
    • 2020-02-28
    相关资源
    最近更新 更多