I have been using some of the html from the cs50 finance example page: https://finance.cs50.net/buy (which is permitted by CS50) I think it may be at the root of the problem. Here's the relevant html code for my BUY page:
<form action="/buy" method="post"> <div class="form-group"> <input autocomplete="off" autofocus="" class="form-control" name="symbol" placeholder="Symbol" type="text"> </div> <div class="form-group"> <input autocomplete="off" class="form-control" min="1" name="shares" placeholder="Shares" type="number"> </div> <button class="btn btn-primary" type="submit">Buy</button>
I think the "form-control" and min="1" limits the user to inputting valid numbers of shares.
In any case, my page shows an alert when user tries to enter an invalid/fractional amount shares, like so:
What I am not sure about is where this "enter valid value" response comes from. So my first question is, where does it come from? Is this part of Bootstrap?
In any case, the alert does its job, and prevents the user from entering an invalid value.
HOWEVER, Check50 claims:
So my second question is, does the alert send a "200" message, and if so, how do I change it to send a "400" message?
And if not, what else could the problem be?
Here's how the relevant part of my Buy function looks:
@login_required def buy(): if request.method == "POST": stock = lookup(request.form.get("symbol")) # Ensure valid symbol was submitted if not stock: return apology("Sorry! That symbol doesn't exist", 400) shares = int(request.form.get("shares"))
I think I could solve the problem by getting rid of the min=1 and then putting an if statement in my function along the lines of:
if not shares or (shares % 1 !=0) or shares < 0:
But I much prefer the current way it works, if only CS50 were happy with it! :P
Much obliged for any help you can provide! :)