Issue
I'm trying to build a budget tracker website using Python and Flask where you can keep track of how you spend your money. I've included a section where you select the type of spending, but when i try to retrieve the user selection using Flask the request.form.get()
returns None
.
I also tried to store the selected variable in a session but it does not work and I tried the request.form\["selected"\]
but it gives me a 400 error.
Anyone knows how I can fix this issues?
Here is my HTML:
<form action="/" method="post" enctype="multipart/form-data">
<h3 class="h3">Your expenses</h3>
<div class="top">
<input type="number" class="form-control mx-auto w-auto" placeholder="$0.00" aria-describedby="button-addon2">
</div>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">Add Expense</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Category: </h1>
<div>
<select id="category-select" class="form-select" aria-label="Default select example">
<option id="selected" name="selected" selected>Select type</option>
<option id="home" name="home" value="0">Home</option>
<option id="loans" name="loans" value="1">Loans</option>
<option id="entertainment" name="entertainment" value="2">Entertainment</option>
<option id="transportation" name="transportation" value="3">Transportation</option>
<option id="other" name="other" value="5">Other</option </div>
</select>
</div>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="number" id="amount" name="amount" class="form-control" placeholder="$0.00" aria-describedby="button-addon2" min="1">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" id="addBtn" class="btn btn-primary">Add money</button>
</div>
</div>
</div>
</div>
</form>
Here is my Python code below:
@app.route("/", methods=["GET", "POST"])
@login_required
def index():
if request.method == "GET":
return render_template("index.html")
else:
# Get data from user input
user_id = session["user_id"]
date = datetime.datetime.now()
# Check user cash
usr_cash = db.execute("SELECT cash FROM users WHERE id = ?", user_id)[0]["cash"]
# Check expense amount
amount = int(request.form.get("amount"))
# Check if user has enough money for expense
if usr_cash < amount:
return apology("Not enough money for this expense! Try adding more income")
# Update user cash after expense
updt_cash = usr_cash - amount
# Update database
db.execute("UPDATE users SET cash = ? WHERE id = ?", updt_cash, user_id)
# Create transactions list
selected = request.form.get("selected")
if selected == "Home":
db.execute("INSERT INTO category (home, user_id, date) VALUES (?, ?, ?)", amount, user_id, date)
elif selected == "Loans":
db.execute("INSERT INTO category (loans, user_id, date) VALUES (?, ?, ?)", amount, user_id, date)
elif selected == "Entertainment":
db.execute("INSERT INTO category (entertainment, user_id, date) VALUES (?, ?, ?)", amount, user_id, date)
elif selected == "Transportation":
db.execute("INSERT INTO category (transportation, user_id, date) VALUES (?, ?, ?)", amount, user_id, date)
elif selected == "Other":
db.execute("INSERT INTO category (other, user_id, date) VALUES (?, ?, ?)", amount, user_id, date)
print(selected)
return render_template("index.html")
Solution
Your select
is incorrect.
You only need value
in your options
. Get rid of id
and name
.
Also flask is looking for a name for your field. Add one to this:
<select id="category-select"
Answered By - vintastic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.