【发布时间】:2018-08-14 23:44:36
【问题描述】:
books() 方法中的命令基于已经有效的代码。 INSERT INTO 语句是问题所在。当 HTML 表单提交(屏幕底部)时,输入字段文本框中的 userreview 不会被我的 books() 方法拉出并插入到我的 postgres 数据库中,尽管存在文本。为了检查 HTML 页面中的任何内容是否被正确拉入,我测试了隐藏的book_id,它被books() 方法拉出并按预期插入到数据库中。
知道为什么这会坚决拒绝将输入字段中的文本提取到我的方法中并将其插入到我的数据库中吗?
@app.route("/books", methods=["POST"])
@login_required
def books():
book_id = request.form.get("bookid")
review = request.form.get("userreview")
db.execute("INSERT INTO test (review, bookid) VALUES (:review, :bookid)", {"review": review, "bookid":book_id})
db.commit()
{% block body %}
<div>
<table class="table">
<thead>
<tr>
<th><strong>Username</strong></th>
<th><strong>Review</strong></th>
<th class="text-center"><strong>Rating</strong></th>
</tr>
</thead>
{% for review in reviews %}
<tr>
<td>{{ review.username }}</td>
<td>{{ review.reviews }}</td>
<td class="text-center">{{ review.rating }}</td>
</tr>
{% endfor %}
</table>
</div>
<form action="{{ url_for('books') }}", method="post">
<div class="form-group">
<input name="userreview" type="text" class="form-control" id="bookreview">
<input type="hidden" name="bookid" value="{{ books.id }}" />
</div>
<button type="button" class="btn btn-primary">Submit</button>
</form>
【问题讨论】:
标签: postgresql flask flask-sqlalchemy