我的Django Python代码没有更改数据库记录
我正在创建一个拍卖网站,我希望能够更改表中的数据,以更改新出价的价格。我是Django和Python的新手,所以我肯定错过了什么。现在我没有收到任何错误-只是没有更改数据库中的数据。有人看到这个问题吗 下面是我的HTML和Python代码(相关部分)。我还添加了urls.py文件,以防需要在那里添加一些东西进行投标?我不这么认为,因为它没有链接到另一个页面 URL.py:我的Django Python代码没有更改数据库记录,python,django,database,sqlite,Python,Django,Database,Sqlite,我正在创建一个拍卖网站,我希望能够更改表中的数据,以更改新出价的价格。我是Django和Python的新手,所以我肯定错过了什么。现在我没有收到任何错误-只是没有更改数据库中的数据。有人看到这个问题吗 下面是我的HTML和Python代码(相关部分)。我还添加了urls.py文件,以防需要在那里添加一些东西进行投标?我不这么认为,因为它没有链接到另一个页面 URL.py: urlpatterns = [ path("", views.index, name="
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("auction", views.auction, name="auction"),
path("watchlist/<str:username>", views.watchlist, name="watchlist"),
path("categories", views.categories, name="categories"),
path("post/<str:title><str:description><int:price><str:category>", views.post, name="post"),
path("bid/<str:username>", views.bid, name="bid")
]
models.py:
class Bid(models.Model):
itemID = models.CharField(max_length=64)
newBid = models.IntegerField()
highestBidder = models.CharField(max_length=64)
post.html:
<h2>{{ p.title }} - Category: {{ p.category }}</h2>
<p>Price = ${{ p.price }}</p> <div>
{% if user.is_authenticated %}
<form name="bid"
action="/bid/{{user.username}}"
method="post" >
{% csrf_token %}
<input autofocus class="form-control" type="text" name="newBid" placeholder="Enter New Bid">
<input autofocus class="form-control" type="hidden" name="itemID" value={{p.title}}{{p.price}}>
<input autofocus class="form-control" type="hidden" name="price" value={{p.price}}>
<input autofocus class="form-control" type="hidden" name="title" value={{p.title}}>
<input class="btn btn-primary" type="submit" value="Place Bid">
</form>
{{p.title}-类别:{{p.Category}
价格=${p.Price}}
{%if user.u经过身份验证%}
{%csrf_令牌%}
query2=NewPost.objects.filter(title=title.first().update(price=newpid)
update
方法仅用于queryset(多个实例),删除.first()
,并将用新的价格更新所有具有该标题的NetPost
像这样:
NewPost.objects.filter(title=title.update(price=newBid)
我没有在视图中导入出价模型。py我尝试了,但仍然遇到同样的问题-它没有任何功能-但是没有错误。你没有与视图匹配的url。我必须创建一个Bid.html页面吗?我在上面更新了我的视图,但没有发生任何事情更改此url路径(“bid”,views.bid,name=“bid”)
对于此路径(“bid/”,views.bid name=“bid”),
有效!我知道这很简单。谢谢我认为您的if语句可能无法正常工作:if(newBid>price and username!=query.highestBidder)
您确定总是username!=查询。最高投标人
。尝试暂时删除if语句,然后查看数据库是否已更新。应该可以吗?如果查询没有任何内容,那么它将是无内容的。如果用户名!=没有,那么它就会起作用。似乎唯一可行的方法是用户名不匹配。它也不适用于if语句。我甚至拿出了代码,只放了username=“hi”和print(username),当我单击bid时,它甚至没有打印出来。出于某种原因,它没有激活出价。我是否需要在我的URL.py文件中加入出价,即使它不会指向新链接?为什么它不会激活出价?你确定你的表格正在提交吗?
<h2>{{ p.title }} - Category: {{ p.category }}</h2>
<p>Price = ${{ p.price }}</p> <div>
{% if user.is_authenticated %}
<form name="bid"
action="/bid/{{user.username}}"
method="post" >
{% csrf_token %}
<input autofocus class="form-control" type="text" name="newBid" placeholder="Enter New Bid">
<input autofocus class="form-control" type="hidden" name="itemID" value={{p.title}}{{p.price}}>
<input autofocus class="form-control" type="hidden" name="price" value={{p.price}}>
<input autofocus class="form-control" type="hidden" name="title" value={{p.title}}>
<input class="btn btn-primary" type="submit" value="Place Bid">
</form>