skip to Main Content

Very new to this – teaching myself Python/flask etc.
I have code trying to make my own word guessing game. I have html code to produce a keyboard and return a clicked key. It works fine returning to app route but crashes with "Method Not Allowed The method is not allowed for the requested URL." if I change the return redirect from / to /goPlay in the def key() code at the end of the python code.

Here is python main code

""" rebuild LockUp """

from flask import Flask, render_template, request,session,redirect

from functionsLockup import pickWord,translateWord

# not needed intil lanuage select is enabled
# from functionsLockup import translateWord

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
@app.route("/")
def jailMan():
    """ start here - collect game control info from the player """
#    We need to add words in language for each language and pass over

    return render_template('index.html',newPlayer=True)

@app.route('/goPlay', methods=['POST'])

def goPlay():
    """ function starts a game with the named user """
    class game:
        """ class stores the game control and result data """

        def __init__(
                self, playerName, theWord,theLanguage,theGuessedWord,
                guessCount,maxGuessCount,wrongGuessCount,
                gamesPlayed,gamesWon
                ):
            self.playerName = playerName
            self.theWord = theWord
            self.language = theLanguage
            self.theGuessedWord = theGuessedWord
            self.guessCount = guessCount
            self.maxGuessCount = maxGuessCount
            self.wrongGuessCount = wrongGuessCount
            self.gamesPlayed=gamesPlayed
            self.gamesWon=gamesWon
            self.newGame=True
            self.keyPressed = key

    # # set up the object for this game
    thisGame=game
    # Go pick and save a word
    theWordSelected = pickWord()

    # add translation switch here
    if request.method == "POST":
        thisGame.playerName=request.form['playerName']
        thisGame.language=request.form['language']

    theWordSelected = translateWord(theWordSelected,thisGame.language)
    # and set up the blank copy of that word
    thisGame.theWord=theWordSelected
    thisGame.theGuessedWord = thisGame.theWord.replace(theWordSelected, "_" * len(theWordSelected))
    thisGame.guessCount=0
    thisGame.maxGuessCount=8
    thisGame.wrongGuessCount=0
    thisGame.gamesPlayed=0
    thisGame.guessCount=0
    thisGame.newGame=True
    # thisGame.keyPressed = session["key"]

    # now go play
    if request.method == "POST":

        if request.form['btnGoPlay'] == "GoPlay":
            return render_template('goPlay.html',theGame=thisGame)
        return render_template('goExit.html')


@app.route("/key", methods=["POST"])
def key():
    session["key"]  = request.form.get("key")
    return redirect("/goPlay")    

if __name__ == "__main__":
    app.run(debug=True)

And here is the keyboard code (though that might not be relevant).

{% block content %}
<div class="clsKeyboard">
    <form class="clsKBForm"  action="/key" method="POST">
        <!-- Numbers - for some languages -->
        <div class="clsRow clsRow0">
            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>


            <button class="clsKey" name="key">&nbsp;</button>

        </div>
        <!-- Row 1 -->
        <div class="clsRow clsRow1">
            <button class="clsKey" name="key" value="Q" type="submit">Q</button>
            <button class="clsKey" name="key" value="W" type="submit">W</button>
            <button class="clsKey" name="key" value="E" type="submit">E</button>
            <button class="clsKey" name="key" value="R" type="submit">R</button>
            <button class="clsKey" name="key" value="T" type="submit">T</button>
            <button class="clsKey" name="key" value="Y" type="submit">Y</button>
            <button class="clsKey" name="key" value="U" type="submit">U</button>
            <button class="clsKey" name="key" value="I" type="submit">I</button>
            <button class="clsKey" name="key" value="O" type="submit">O</button>
            <button class="clsKey" name="key" value="P" type="submit">P</button>
        </div>
        <!-- Row 2 -->
        <div class="clsRow clsRow2">
            <button class="clsKey" name="key" value="A" type="submit">A</button>
            <button class="clsKey" name="key" value="S" type="submit">S</button>
            <button class="clsKey" name="key" value="D" type="submit">D</button>
            <button class="clsKey" name="key" value="F" type="submit">F</button>
            <button class="clsKey" name="key" value="T" type="submit">G</button>
            <button class="clsKey" name="key" value="H" type="submit">H</button>
            <button class="clsKey" name="key" value="J" type="submit">J</button>
            <button class="clsKey" name="key" value="K" type="submit">K</button>
            <button class="clsKey" name="key" value="L" type="submit">L</button>
        </div>
        <!-- Row 3 -->
        <div class="clsRow  clsRow3">
            <button class="clsKey" name="key" value="Z" type="submit">Z</button>
            <button class="clsKey" name="key" value="X" type="submit">X</button>
            <button class="clsKey" name="key" value="C" type="submit">C</button>
            <button class="clsKey" name="key" value="V" type="submit">V</button>
            <button class="clsKey" name="key" value="B" type="submit">B</button>
            <button class="clsKey" name="key" value="N" type="submit">N</button>
            <button class="clsKey" name="key" value="M" type="submit">M</button>
            <button class="clsKey" name="key" value="P" type="submit">P</button>
        </div>
    </form>
</div>
{% endblock %}

Stuck! Can anyone give me a hand here please??

Nick

2

Answers


  1. When you’re doing a redirect at the end of your key route you’re changing to a GET request which isn’t allowed on your goPlay route.

    Login or Signup to reply.
  2. You need to add a route decorator for the "/goPlay" endpoint that handles GET requests. For example:

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

    This will allow the "/goPlay" endpoint to handle both POST and GET requests.

    When a GET request is made to this endpoint, the goPlay() function will be called and will return the appropriate response.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search