0

I want to make add cash button on my cs50 finance, I have made separate html file for this and made a separate route for this but when I run it, It takes me straight to apology without showing me html form at all.Here is my python code.

@app.route("/addcash", methods=["GET", "POST"])
@login_required
def addcash():
#For personal touch add cash to user
if request.method == "POST":
    #Take amount to be added
    numb = request.form.get("amount")
    if numb is None or numb == '' or numb < 1:
        return apology ("Please enter valid amount")
    else:
        #Take user cash
        row = db.execute ("select cash from users where id = :ii" , ii = session["user_id"])
        #Now add the amount to users data base.
        db.execute("UPDATE users SET cash = :cash WHERE id = :ide",cash = row[0]["cash"] + numb, ide = session["user_id"])
    return redirect(url_for("index"))    

else:
    #This programe is taking me straight here.
    return apology("Invalid mathod")

And here is my html

{% extends "layout.html" %}

{% block title %}
Please enter the amount you want to add!
{% endblock %}

{% block main %}
<form action="{{ url_for('addcash') }}" method="post">
    <fieldset>
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="amount" placeholder="amount" type="text"/>
        </div>
        <div class="form-group">
            <button class="btn btn-default" type="submit">Add</button>
        </div>
    </fieldset>
</form>
{% endblock %}

2 Answers 2

2

Flask is doing what you told it to do. There is a line to return an apology. Instead, do something like

return render_template("addcash.html")

or whatever your template's name is.

1

It's going straight to the 'else' option because when the page initally loads, it's not actually a 'POST' request. It's a 'GET' request.

With GET you are retrieving the .hmtl file to be loaded. So, as the answer above stated, you need to return the template of your html file with the else statement.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .