如何在Flask Python中修复“找不到此页面”?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (36)

我正在Flask中为API设置一个新服务器。还有一个服务器来渲染Flask的前端。好吧,所以当我向确定API路线发出请求时,我得到了这个奇怪的回答“找不到这个页面”。我似乎真的好好怎样才能调试这个bug?其他奇怪的是它总是给我200状态。错误在路线中:@ app.route('/ v1.0 / aluno / update /',methods = ['POST'])

API SIDE


@app.route('/v1.0/aluno/<int:aluno_id>', methods=['GET'])
def aluno(aluno_id):
    if request.method == 'GET':
        cur = mysql.connection.cursor()
        query = "SELECT NOME, NUMERO, PASSWORD FROM aluno WHERE NUMERO=%s"
        cur.execute(query, (aluno_id,))
        data = cur.fetchall()
        if len(data) <= 0:
            return Response(status=404)
        else:
            aluno = {
                'nome': data[0][0],
                'numero': data[0][1],
                'password': data[0][2]
            }
            js = json.dumps(aluno)
            resp = Response(js, status=200, mimetype='application/json')
            resp.headers['Links'] = 'http://127.0.0.1/aluno'
            return resp

@app.route('/v1.0/aluno/delete/<int:aluno_id>', methods=['POST'])
def aluno_delete(aluno_id):
    if request.method == 'POST' and request.form['_method'] == 'delete':
        query = "DELETE FROM aluno WHERE NUMERO = %s"
        cur = mysql.connection.cursor()
        cur.execute(query, (aluno_id,))
        mysql.connection.commit()
        cur.fetchall()
        cur.close()
        return Response(status=200)

@app.route('/v1.0/aluno/update/<int:aluno_id>', methods=['POST'])
def aluno_update(aluno_id):
    form = AlunoForm(request.form)
    if request.method == 'POST' and form.validate():
        nome = request.form["nome"]
        numero = request.form["numero"]
        password = request.form["password"]
        cur = mysql.connection.cursor()
        query = "UPDATE aluno SET NOME=%s, NUMERO=%s, PASSWORD=%s WHERE NUMERO = %s"
        cur.execute(query, (nome, numero, password, aluno_id))
        mysql.connection.commit()
        cur.execute(
            "SELECT NOME, NUMERO FROM aluno WHERE NUMERO = %s", (aluno_id,))
        data = cur.fetchall()
        cur.close()

        print(" * DATA ")
        print(data)

        aluno = {
            'nome': data[0][0],
            'numero': data[0][1]
        }

        js = json.dumps(aluno)
        resp = Response(js, status=200, mimetype='application/json')
        resp.headers['Links'] = 'http://127.0.0.1/aluno'
        return resp
    elif request.method == 'POST' and not form.validate():
        resp = Response(status=400)
        resp.headers['Links'] = 'http://127.0.0.1/aluno'
        return resp

前端


{% endblock %}


<script type="text/javascript" src="{{url_for('static', filename='js/jquery-3.2.1.min.js') }}"></script>

<script type="text/javascript" src="{{url_for('static', filename = 'js/bootstrap.min.js')}}"></script>

<script>
    function aluno_update(){
        try{
            let formElement = document.getElementById("aluno_update")
            //let formData = formElement.
            //console.log(formData)
            $.ajax({
                type: "POST",
                url: "http://127.0.0.1:80/v1.0/aluno/update/{{aluno['numero']}}",
                data: {'nome': 'João Luis','numero':'16172','password':'Password'},
                //dataType: 'json',
                success: function(data){
                    //location.href = "http://127.0.0.1:3000/v1.0/alunos/"
                    alert(data)
                    console.log(data)
                },
                error(jqXHR,JQueryXHR,errorThrown){
                    //console.log(formData)
                    alert(jqXHR)
                    alert(JQueryXHR)
                    alert(errorThrown)
                    console.log(jqXHR)
                    console.log(JQueryXHR)
                    console.log(errorThrown)
                }
            })
        }catch(err){
            alert(err)
        }
    }
</script>

我尝试过使用POSTMAN而不是常规的Web浏览器。向API发出请求。但我得到相同的回答:'找不到此页面'

至少我期待某种400 Bad Request或类似的东西。

https://github.com/joaogracio/SqlParser

提问于
用户回答回答于

表单验证失败,您将根据以下代码获得400

   elif request.method == 'POST' and not form.validate():
        resp = Response(status=400)
        resp.headers['Links'] = 'http://127.0.0.1/aluno'
        return resp

扫码关注云+社区

领取腾讯云代金券