Home > database >  input not working trying to redirect from "/' to "deal"
input not working trying to redirect from "/' to "deal"

Time:10-21

Hello I am building a blackjack game and I am stuck the deal button is not working and I am trying to get that to redirecting to another URL. Also do I need that many variables to update the game as the client is playing ? This is my third day doing flask after and my first web application. Any help or pointing me in the right direction would be appreciated here is the HTML code for the inputs as well as my starting code on flask

    <form action="/deal" method="Post" >
    <input  type="submit" value="Hit" name="Hit"/>
    <input  type="submit" value="Stay" name="Stay"/>
    </form>
    <form action="/" method="Post" >
    <input  type="submit" value="Deal" name="Deal"/>
    </form>

    from flask import Flask, render_template, request, Response, redirect, url_for
    import random
    import main
    import datetime as dt
    
    app = Flask(__name__)
    
    year = dt.datetime.now().year
    count = 0
    
    
    @app.route('/', methods=['POST', "GET"])
    def home():
        global count
        if count == 0:
            count  = 1
            return render_template("index.html", year=year)
        elif request.method == "GET":
            if request.form.get("Stay", False):
                return redirect(url_for('/deal'))
    
    
    @app.route('/deal', methods=['POST', "GET"])
    def deal():
        if len(main.player_hand) == 0:
            main.start()
            main.get_current_score(main.player_hand)
            return render_template("index.html", year=year, player=main.player_hand, dealer=main.dealer_hand[1:],
                                   score_d=main.get_dealer_starting_value(main.dealer_hand),
                                   score_p=main.get_current_score(main.player_hand))
        if request.method == "POST":
            if request.form.get("Hit", False):
                if main.get_current_score(main.player_hand) <= 21:
                    main.player_hand_dealt()
                    return render_template("index.html", year=year, player=main.player_hand, dealer=main.dealer_hand[1:],
                                           score_d=main.get_dealer_starting_value(main.dealer_hand),
                                           score_p=main.get_current_score(main.player_hand))

CodePudding user response:

Clicking the Deal button will submit a POST to / with Deal=Deal in the post body as Content-Type: application/x-www-form-urlencoded

Your if condition inside of the home route only redirects to /deal if the request is a GET and if the request form body has a value for Stay. This should be changed to if request.method == "POST": and request.form.get("Deal")

or

if you want the form to submit a GET, then the form's markup should have method="GET" and in the home route, you should look for the request data in the query parameters via request.args.get("Deal").

  • Related