Search code examples
pythonflaskpythonanywhere

Python @app.route("/custom"), return page doesn't work


I am using Pythonanywhere.com with Flask. I am using this code for the html:

    <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>

I am using this python code:

@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")

When I click on the submit button, it returns "Bad Request The browser (or proxy) sent a request that this server could not understand." So this happends when I click on the button on my domainname.com/quiz. I noticed that when I clicked on the button, the "/quiz" behind the domain name dissapeard. What am I doing wrong?


Solution

  • Change action="." to action="#".

    action="." causes the request to be sent to one level above the current url which is why /quiz disappear.