2017-06-18 50 views
0

我正在使用Pythonanywhere.com和Flask。我使用此代碼爲HTML:Python @ app.route(「/ custom」),返回頁面不起作用

<div class="main-content"> 

     <form class="main_page" method="POST" action="."> 

      <div class="form-title-row"> 
       <h1>Aankomende Games Quiz</h1> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Jouw naam:</span> 
        <input type="text" name="naam"> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game heeft géén multiplayer?</span> 
        <select name="geen_multiplayer"> 
         <option selected disabled>Maak een keuze</option> 
         <option>Red Dead Redemption 2</option> 
         <option>Destiny 2</option> 
         <option>South Park: The Fractured but Whole</option> 
         <option>FIFA 18</option> 
        </select> 
       </label> 
      </div> 


      <div class="form-row"> 
       <label> 
        <span>Welke game is door Rockstar Games gemaakt?</span> 
        <select name="gemaakt_rockstar"> 
         <option selected disabled>Maak een keuze</option> 
         <option>Star Wars Battlefront II</option> 
         <option>The Elder Scrolls Online: Morrowind</option> 
         <option>Destiny 2</option> 
         <option>Red Dead Redemption 2</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Wat is géén shooter?</span> 
        <select name="geen_shooter"> 
         <option selected disabled>Maak een keuze</option> 
         <option>Red Dead Redemption 2</option> 
         <option>Destiny 2</option> 
         <option>Call of Duty: WWII</option> 
         <option>Star Wars Battlefront II</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game wordt alleen op PS4 uitgebracht?</span> 
        <select name="alleen_ps4"> 
         <option selected disabled>Maak een keuze</option> 
         <option>MotoGP™17</option> 
         <option>Crash Bandicoot N. Sane Trilogy</option> 
         <option>Star Wars Battlefront II</option> 
         <option>Destiny 2</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game heeft splitscreen?</span> 
        <select name="heeft_splitscreen"> 
         <option selected disabled>Maak een keuze</option> 
         <option>MotoGP™17</option> 
         <option>Dirt 4</option> 
         <option>Red Dead Redemption 2</option> 
         <option>The Elder Scrolls Online: Morrowind</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game is door Codemasters gemaakt?</span> 
        <select name="gemaakt_codemasters"> 
         <option selected disabled>Maak een keuze</option> 
         <option>South Park: The Fractured but Whole</option> 
         <option>FIFA 18</option> 
         <option>Crash Bandicoot N. Sane Trilogy</option> 
         <option>Dirt 4</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game speelt zich af in het wilde westen?</span> 
        <select name="wilde_westen"> 
         <option selected disabled>Maak een keuze</option> 
         <option>Dirt 4</option> 
         <option>Call of Duty: WWII</option> 
         <option>Red Dead Redemption 2</option> 
         <option>The Elder Scrolls Online: Morrowind</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game heeft 'Sports' niét als genre</span> 
        <select name="niet_sports"> 
         <option selected disabled>Maak een keuze</option> 
         <option>FIFA 18</option> 
         <option>Dirt 4</option> 
         <option>Destiny 2</option> 
         <option>MotoGP™17</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game is voor 18 jaar en ouder?</span> 
        <select name="voor_18"> 
         <option selected disabled>Maak een keuze</option> 
         <option>The Elder Scrolls Online: Morrowind</option> 
         <option>Star Wars Battlefront II</option> 
         <option>Destiny 2</option> 
         <option>Crash Bandicoot N. Sane Trilogy</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
       <label> 
        <span>Welke game komt in september 2017 uit?</span> 
        <select name="september_2017"> 
         <option selected disabled>Maak een keuze</option> 
         <option>Red Dead Redemption 2</option> 
         <option>South Park: The Fractured but Whole</option> 
         <option>Call of Duty: WWII</option> 
         <option>FIFA 18</option> 
        </select> 
       </label> 
      </div> 

      <div class="form-row"> 
        <button type="submit" name="invoer" value="">Invoeren</button> 
      </div> 
     </form> 
    </div> 

</body> 

我使用這個Python代碼:

@app.route("/quiz", methods=["GET", "POST"]) 

def quiz(): 

    if request.method == "GET": 
     return render_template("quiz_page.html") 

    score = 0 


    if (request.form["geen_multiplayer"] == "South Park: The Fractured but Whole"): 
     score += 1 

    if (request.form["gemaakt_rockstar"] == "Red Dead Redemption 2"): 
     score += 1 

    if (request.form["geen_shooter"] == "Red Dead Redemption 2"): 
     score += 1 

    if (request.form["alleen_ps4"] == "Crash Bandicoot N. Sane Trilogy"): 
     score += 1 

    if (request.form["heeft_splitscreen"] == "MotoGP™17"): 
     score += 1 

    if (request.form["gemaakt_codemasters"] == "Dirt 4"): 
     score += 1 

    if (request.form["wilde_westen"] == "Red Dead Redemption 2"): 
     score += 1 

    if (request.form["niet_sports"] == "Destiny 2"): 
     score += 1 

    if (request.form["voor_18"] == "The Elder Scrolls Online: Morrowind"): 
     score += 1 

    if (request.form["september_2017"] == "FIFA 18"): 
     score += 1 

    if request.method == "POST": 
     return render_template("quiz_page.html") 

當我點擊提交按鈕,返回「錯誤的請求 瀏覽器(或代理)發送了一個這個服務器無法理解的請求。「所以當我點擊我的domainname.com/quiz上的按鈕時會發生這種情況。我注意到當我點擊按鈕時,域名背後的「/ quiz」消失。我究竟做錯了什麼?

+0

在你的源代碼中,你還有裝飾器和函數之間的空行嗎? – vZ10

回答

2

更改action="."action="#"

action="."導致請求被髮送到當前url上面的一個級別,這就是爲什麼/quiz消失。

+0

非常感謝,有效:) – Jip1912

+0

另一種方法 - 使用'@ app.route(「/ quiz /」,methods = [「GET」,「POST」])(注意「測驗」後面的斜線)然後你可以使用'action =「。」'。 –