As last task to do for pset7, "personal touch", I'd like to empower the user to buy and sell shares via index without having to type the stock symbol.
From here I found interesting to create a form for each row within the index table, a form which allows the user to type the number of shares he/she wants to sell/buy, click the respective button and see the result.
The following is the form I put in index.html
<tbody>
{% for purchase in purchases %}
{% if purchase.shares != 0 %}
[SHOW USER'S STOCK PORTFOLIO]
<td>
<form action="{{url_for('buy')}}" method="POST">
<fieldset>
<div class="form-group">
<input class="input -xs" autocomplete="on" autofocus class="form-control" name="{{purchase.symbol}}" placeholder="Shares" type="number"/>
</div>
<div class="form-group">
<button class="btn btn-secondary btn-xs" type="submit" formaction="{{url_for('buy')}}" formmethod="POST">Buy</button>
</div>
</div>
</fieldset>
</form>
</td>
</tr>
{% endif %}
{% endfor %}
<tr>
<td colspan="4">CASH</td>
<td> {{ "${:,.2f}".format(net) }}</td>
</tr>
</tbody>
From here I don't know how to manage the data sent to /buy.
I tried this:
# if route was reached via POST
if request.method=="POST":
# if request was sent via /buy
if request.form.get("symbol")
[buy stocks via /buy]
# if request was sent via /index
if request.form.get("{{purchase.symbol}}"):
#check if it works
return apology("keep it up!")
But I get the error message "ValueError: View function did not return a response"
Any advice would be much appreciated! I really want to understand how the logic to solve this problem. `
"{{purchase.symbol}}"
won't work in the python file, Jinja will replace that one with the actual value at rendering time. Use a fixed name for the field (and maybe another, hidden, input field to also pass the symbol, again with a fix name). Also, does your function always return something? And instead of{{ "${:,.2f}".format(net) }}
you can use helper filterusd
like{{ net | usd }}