[英]How to get row data on form submission when the cell is not an input field
I am working on problem set 9 (Finance) and trying to add my personal touch (allow users to buy more shares or sell shares of a stock they already own via index itself, without having to type stocks' symbols manually).我正在处理问题集 9(金融)并尝试添加我的个人风格(允许用户通过指数本身购买更多股票或出售他们已经拥有的股票的股票,而无需手动输入股票的符号)。 That being said, I have the following HTML:
话虽如此,我有以下 HTML:
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<table class="table table-striped center">
<thead>
<tr>
<th class="text-start">Symbol</th>
<th class="text-start">Name</th>
<th class="text-end">Shares</th>
<th class="text-end">Price</th>
<th class="text-end">TOTAL</th>
<th class="text-middle">Buy/Sell</th>
</tr>
</thead>
<tbody>
{% for stock in stocks %}
<form action="/" method="post">
<tr id="stock_{{ loop.index }}">
<td class="text-start" name="symbol" id="symbol">{{ stock["symbol"] }}</td>
<td class="text-start">{{ stock["name"] }}</td>
<td class="text-end">{{ stock["shares"] }}</td>
<td class="text-end">{{ stock["price"] | usd }}</td>
<td class="text-end">{{ ((stock["price"]) * (stock["shares"])) | usd }}</td>
<td class="text-middle">
<input type="number" min="1" name="shares" placeholder="shares" style="width:80px">
<button type="submit" name="action" value="sell" class="btn btn-primary button">Sell</button>
<button type="submit" name="action" value="buy" class="btn btn-primary button">Buy</button>
</td>
</tr>
</form>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td colspan="4" class="text-end"><b>Cash</b></td>
<td class="text-end">{{ cash[0]["cash"] | usd }}</td>
</tr>
<tr>
<td colspan="4" class="text-end"><b>TOTAL</b></td>
<td class="text-end">{{ total | usd }}</td>
</tr>
</tfoot>
</table>
<script>
document.querySelector("form")addEventListener("submit", function() }
let value = getElementById("#symbol");
console.log(value);
alert(value);
)};
</script>
{% endblock %}
That will create the following webpage:这将创建以下网页:
My tables are dynamically generated via a list of dictionaries called stocks
that is passed in from my app.py
controller.我的表格是通过从我的
stocks
controller 传入的app.py
stock 的字典列表动态生成的。 I feel it was easiest to put a form submission on each table row and not the table as a whole so I can know which row was actually submitted when the buy
or sell
button was clicked.我觉得将表单提交放在每个表格行而不是整个表格上是最简单的,这样我就可以知道单击
buy
或sell
按钮时实际提交了哪一行。 I do not know how to pass the data from a cell that is not an input field, in this case <td class="text-start" name="symbol" id="symbol">{{ stock["symbol"] }}</td>
so that I can use that information on the server-side to be able to get all other relevant information about the stock.我不知道如何从不是输入字段的单元格传递数据,在这种情况下
<td class="text-start" name="symbol" id="symbol">{{ stock["symbol"] }}</td>
这样我就可以在服务器端使用该信息来获取有关股票的所有其他相关信息。 I get a TypeError: quote_from_bytes() expected bytes
from my code here for the line request.form.get("symbol")
:我从我的代码中得到
TypeError: quote_from_bytes() expected bytes
行request.form.get("symbol")
:
@app.route("/", methods=["POST", "GET"])
@login_required
def index():
"""Show portfolio of stocks"""
# Get users cash balance
cash = db.execute("SELECT cash FROM users WHERE id = ?", session["user_id"])
total = cash[0]["cash"]
# Make sure stocks table exists (will only be applicable until first stock purchase on site)
if (db.execute("SELECT name FROM sqlite_master WHERE type = ? AND name = ?", "table", "stocks")):
if request.method == "POST":
# Run checks on input
if not request.form.get("shares"):
return apology("Missing shares")
if int(request.form.get("shares")) < 1:
return apology("Please input positive shares")
# Look up stocks current price
shares = float(request.form.get("shares"))
print(shares)
stock = lookup(request.form.get("symbol"))
print(stock)
price = float(stock["price"])
print(price)
I believe grabbing request.form.get("symbol")
doesn't make sense since its not part of the form data?我相信抓取
request.form.get("symbol")
没有意义,因为它不是表单数据的一部分? Is there anyway to get the value in that cell to pass to my server-side?无论如何要让该单元格中的值传递给我的服务器端? And yes the JavaScript at the end was an attempt to try and grab that data to maybe modify a value upon submission that I could then use, but I am not sure.
是的,最后的 JavaScript 试图尝试获取该数据,以便在提交时修改一个值,然后我可以使用,但我不确定。
Thank you to the comments I have figured this out.感谢您的评论,我已经弄清楚了。 I realize this wasnt the best asked question.
我意识到这不是最好的问题。 I have implemented another
readonly
field and adjusted the form to be for the single cell as follows:我已经实现了另一个
readonly
字段并将表单调整为单个单元格,如下所示:
<tbody>
{% for stock in stocks %}
<tr id="stock_{{ loop.index }}">
<td class="text-start">{{ stock["symbol"] }}</td>
<td class="text-start">{{ stock["name"] }}</td>
<td class="text-end">{{ stock["shares"] }}</td>
<td class="text-end">{{ stock["price"] | usd }}</td>
<td class="text-end">{{ ((stock["price"]) * (stock["shares"])) | usd }}</td>
<td class="text-middle">
<form action="/" method="post">
<input type="number" min="1" name="shares" placeholder="shares" style="width:80px">
<input type="text" readonly name="symbol" value="{{ stock['symbol'] }}" style="width:80px">
<button type="submit" name="action" value="sell" class="btn btn-primary button">Sell</button>
<button type="submit" name="action" value="buy" class="btn btn-primary button">Buy</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.