代码之家  ›  专栏  ›  技术社区  ›  Robert Wyszynski

可以获取一个方法,但POST不起作用。你知道为什么POST不使用这个方法吗?

  •  0
  • Robert Wyszynski  · 技术社区  · 2 年前
    from flask import Flask, render_template, url_for, request, redirect
    
    app = Flask(__name__)
    
    @app.route('/doborpompy', methods=['GET', 'POST'])
    def doborpompy():
    
        if request.method == 'GET':
            return render_template('doborpompy.html')
        else:
            metry = 200
            if 'metry' in request.form:
                metry = request.form['metry']
    
            return render_template('doborpompy_wynik.html', metry=metry)
    

    “GET/doborcompy%20method=?methy=44&metry_ogrzewane=44&ilosc_osob=44&aamp;typ_domu=pasywny&dofinasowanie=dofinasoWanie&budge=standard&zrodlo=ogrzewanie_podlogowe&povietrze_woda=woda HTTP/1.1”404-

    我可以选择GET选项,但当我尝试POST时,它仍在尝试执行GET选项。 有什么想法吗?

    3 回复  |  直到 2 年前
        1
  •  1
  •   Mahmoud Dello    2 年前

    你是 处理 岗位 行动 默认情况下,当您访问路线时,您正在执行 得到 请求,所以您应该处理 岗位 这样地:

    from flask import Flask, render_template, url_for, request, redirect
    
    app = Flask(__name__)
    
    @app.route('/doborpompy', methods=['GET', 'POST'])
    def doborpompy():
    
        if request.method == "POST":
            return "<h1>This is how to handle post action in the if statement</h1>"
            # rest of your code ...
        
        return render_template("doborpompy.html")
    
    if __name__ == "__main__":
        app.run()
    

    enter image description here

    你也应该在你的 doborpompy.html 文件:

    <form id="formularz_doboru" action="{{url_for("doborpompy")}}" method="POST">

    <form id="formularz_doboru" action="{{url_for('doborpompy')}}" method="POST">

        2
  •  0
  •   Robert Wyszynski    2 年前
    <h1>Odpowiedz na pytania by dobrać swoją pompę:</h1>
    <form id="formularz_doboru" action="{{url_for("doborpompy")}} method="POST">
    <label for="metry">Podaj ile m2 ma Twój dom</label>
    <input type="text" id="metry" name="metry" value="m2"><br>
    <label for="metry_ogrzewane">Podaj ilość m2 ogrzewanych</label>
    <input type="text" id="metry_ogrzewane" name="metry_ogrzewane" value="m2"><br>
    <label for="ilosc_osob">Podaj ile osób mieszka w domu</label>
    <input type="text" id="ilosc_osob" name="ilosc_osob" value="1-20"><br>
      <label for="typ_domu">Wybierz typ swojego domu:</label>
      <select id="typ_domu" name="typ_domu">
        <option value="niezaizolowany">Niezaizolowany</option>
        <option value="pasywny">Pasywny</option>
        <option value="energooszczędny">Energooszczędny</option>
        <option value="zaizolowany">Zaizolowany</option>
      </select><br>
      <input type="checkbox" id="dofinansowanie" name="dofinansowanie" value="dofinansowanie">
      <label for="dofinansowanie">Będe starał się o dofinansowanie</label><br>
      <label for="budget">Jaki chcesz rodzaj pompy:</label>
      <select id="budget" name="budget">
        <option value="budget">Budget</option>
        <option value="standard">Standard</option>
        <option value="premium">Premium</option>
      </select><br>
      <label for="zrodlo">Jakie jest żródło ogrzewania domu:</label>
      <select id="zrodlo" name="zrodlo">
        <option value="grzejniki">Grzejniki</option>
        <option value="ogrzewanie_podlogowe">Ogrzewanie podłogowe</option>
      </select><br>
      <label for="powietrze_woda">Czy pompa ma być wpowietrzna czy wodna:</label>
      <select id="powietrze_woda" name="powietrze_woda">
        <option value="powietrze">Powietrze</option>
        <option value="woda">Woda</option>
        <option value="nie_wiem">Nie wiem</option>
      </select><br>
    
    <input type="submit" value="Send">
    
    </form>