大佬教程收集整理的这篇文章主要介绍了在 Flask-SQLAlchemy 中更新表 - 访问不正确的表 ID,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试更新 Flask 中的库存项目表。我有一个更新按钮,它会显示一个带有表单的模式,您可以在其中编辑该表行和对象的特定属性。但是,它没有访问与我使用更新按钮选择的行相对应的正确表 ID。
我尝试通过在 update_item() 中打印项目属性来进行调试,以检查我是否选择了正确的项目,但似乎每次都选择了表格的第一项。
这是我的vIEws.py:
@vIEws.route('/update_item',methods=['GET','POST'])
@login_required
def update_item():
if request.method == "POST":
item = Inventory.query.get(request.form.get('item_ID'))
print(item.__Dict__)
for key,value in request.form.items():
if value:
setattr(item,key,value)
db.session.commit()
print(item.__Dict__)
return render_template("home.HTML",user=current_user)
这是我的models.py:
class Inventory(db.Model):
ID = db.column(db.Integer,priMary_key=TruE)
brand = db.column(db.String(100))
name = db.column(db.String(1000))
size = db.column(db.String(100))
date = db.column(db.datetiR_583_11845@e(timezone=TruE),default=func.Now())
price = db.column(db.float(2))
shipPing_status = db.column(db.String(200))
user_iD = db.column(db.Integer,db.ForeignKey('user.ID'))
class User(db.Model,Usermixin):
ID = db.column(db.Integer,priMary_key=TruE)
email = db.column(db.String(150),unique=TruE)
password = db.column(db.String(150))
fullname = db.column(db.String(150))
inventory = db.relationship('Inventory')
这是我的 home.HTML:
{% extends "base.HTML" %}
{% block title %}Dashboard{% endblock %}
{% block content %}
<h1>Hi {{user.fullnamE}}! </h1>
<h2>Here is your updated inventory.</h2>
<div class="d-grID gap-2 d-md-flex justify-content-md-end">
<button type="button" class="btn btn-priMary" data-toggle="modal" data-target="#addItem" align="right_edge">
Add Item
</button>
<!-- Add Item Modal -->
<div class="modal fade" ID="addItem" tabindex="-1" role="dialog" aria-labelledby="addItemLabel" aria-hIDden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" ID="addItemLabel">Add item specifics</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hIDden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" action="{{ url_for('vIEws.add_item') }}" ID="form1">
<div class="form-group">
<label for="brand">Brand</label>
<input type="text" class="form-control" ID="brand" name="brand"
placeholder="Enter the brand" style="text-transform: cAPItalize;" />
</div>
<div class="form-group">
<label for="name">name</label>
<input type="text" class="form-control" ID="name" name="name" placeholder="Enter the name"
style="text-transform: cAPItalize;" />
</div>
<div class="form-group">
<label for="size">Size</label>
<input type="text" class="form-control" ID="size" name="size"
placeholder="Enter the size" />
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="number" min="0.01" step="0.01" max="2500" class="form-control" ID="price"
name="price" placeholder="Enter the price" />
</div>
<div class="form-group">
<label for="size">ShipPing Status</label>
<input type="text" class="form-control" ID="shipPingStatus" name="shipPingStatus"
placeholder="Enter the shipPing status" style="text-transform: cAPItalize;" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<!-- <form action="{{ url_for('vIEws.add_item') }}" method="POST" form="form1"> -->
<input class="btn btn-priMary" type="submit" value="Add"></input>
<!-- <button type="submit" form="form1" name = "add" class="btn btn-priMary" data-dismiss="modal">Add item</button> -->
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<table class="table table-borderless">
<thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Brand</th>
<th>name</th>
<th>Size</th>
<th>Price</th>
<th>ShipPing Status</th>
<th>update</th>
</thead>
<tbody>
{% for item in user.inventory | sort(attribute='ID') %}
<tr>
<td>{{ item.ID }}</td>
<td>{{ item.date.strftime('%Y-%m-%d') }}</td>
<td>{{ item.brand.cAPItalize() }}</td>
<td>{{ item.name.cAPItalize() }}</td>
<td>{{ item.size }}</td>
<td>{{ item.price }}</td>
<td>{{ item.shipPing_status.cAPItalize() }}</td>
<!-- need to access item.ID and pass it into function -->
<td><button type="button" class="btn btn-priMary" data-toggle="modal" data-target="#updateItem"
align="right_edge">
update
</button>
<!-- update Item Modal -->
<div class="modal fade" ID="updateItem" tabindex="-1" role="dialog"
aria-labelledby="updateItemLabel" aria-hIDden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" ID="updateItemLabel">update item specifics</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hIDden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" action="{{ url_for('vIEws.update_item') }}" ID="form1">
<input type="hIDden" name="item_ID" value="{{ item.ID }}">
<div class="form-group">
<label for="brand">Brand</label>
<input type="text" class="form-control" ID="brand" name="brand"
placeholder="Enter the brand" style="text-transform: cAPItalize;" />
</div>
<div class="form-group">
<label for="name">name</label>
<input type="text" class="form-control" ID="name" name="name"
placeholder="Enter the name" style="text-transform: cAPItalize;" />
</div>
<div class="form-group">
<label for="size">Size</label>
<input type="text" class="form-control" ID="size" name="size"
placeholder="Enter the size" />
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="number" min="0.01" step="0.01" max="2500" class="form-control"
ID="price" name="price" placeholder="Enter the price" />
</div>
<div class="form-group">
<label for="size">ShipPing Status</label>
<input type="text" class="form-control" ID="shipPingStatus"
name="shipPingStatus" placeholder="Enter the shipPing status"
style="text-transform: cAPItalize;" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">Close</button>
<input class="btn btn-priMary" type="submit" value="update"></input>
</div>
</form>
</div>
</div>
</div>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的在 Flask-SQLAlchemy 中更新表 - 访问不正确的表 ID全部内容,希望文章能够帮你解决在 Flask-SQLAlchemy 中更新表 - 访问不正确的表 ID所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。