如何检查布尔菲尔德ina django modelform是否为true,并在列表下添加任何内容?
我正在尝试创建一个电子商务网站(CS50项目2),该网站允许用户将清单项目添加到其监视列表中。我已决定将Django的Modelform与布尔菲尔德(Booleanfield)一起使用,该布尔菲尔德(Booleanfield)与WatchList模型相对应。
我已经创建了表单,但我不确定如何检查它是否为真,并将列表保存到监视列表模型。
views.py
def listing(request, id):
listing = Listings.objects.get(id=id)
listing_price = listing.bid
sellar = listing.user
watchlist_form = WatchListForm()
watchlist_form = WatchListForm(request.POST)
if watchlist_form == True:
listing.add()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": comment_form,
"comments": comment_obj,
"bidForm": bid_form,
"bids": bid_obj,
"watchlistForm": watchlist_form
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": comment_form,
"comments": comment_obj,
"bidForm": bid_form,
"bids": bid_obj,
"watchlistForm": watchlist_form
})
models.py
class WatchList(models.Model):
listing = models.ManyToManyField(Listings)
user = models.ForeignKey(User, on_delete=models.CASCADE, default="")
add_to_watchlist = models.BooleanField(default=False)
Listings Models.py
class Listings(models.Model):
CATEGORY = [
("Miscellaneous", "Miscellaneous"),
("Movies and Television", "Movies and Television"),
("Sports", "Sports"),
("Arts and Crafts", "Arts and Crafts"),
("Clothing", "Clothing"),
("Books", "Books"),
]
title = models.CharField(max_length=64)
description = models.CharField(max_length=500)
bid = models.DecimalField(max_digits=1000000000000, decimal_places=2)
image = models.URLField(null=True, blank=True)
category = models.CharField(max_length=64, choices=CATEGORY, default=None)
user = models.ForeignKey(User, on_delete=models.CASCADE, default="")
I am trying to create an e-commerce site (CS50 Project 2) that allows the user to add a listing item to their watchlist. I have decided to use a Django's ModelForm with a BooleanField that corresponds with the WatchList model to do this.
I have created the form but i'm not sure how to check if it is true and save the listing to the WatchList model.
views.py
def listing(request, id):
listing = Listings.objects.get(id=id)
listing_price = listing.bid
sellar = listing.user
watchlist_form = WatchListForm()
watchlist_form = WatchListForm(request.POST)
if watchlist_form == True:
listing.add()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": comment_form,
"comments": comment_obj,
"bidForm": bid_form,
"bids": bid_obj,
"watchlistForm": watchlist_form
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": comment_form,
"comments": comment_obj,
"bidForm": bid_form,
"bids": bid_obj,
"watchlistForm": watchlist_form
})
models.py
class WatchList(models.Model):
listing = models.ManyToManyField(Listings)
user = models.ForeignKey(User, on_delete=models.CASCADE, default="")
add_to_watchlist = models.BooleanField(default=False)
Listings models.py
class Listings(models.Model):
CATEGORY = [
("Miscellaneous", "Miscellaneous"),
("Movies and Television", "Movies and Television"),
("Sports", "Sports"),
("Arts and Crafts", "Arts and Crafts"),
("Clothing", "Clothing"),
("Books", "Books"),
]
title = models.CharField(max_length=64)
description = models.CharField(max_length=500)
bid = models.DecimalField(max_digits=1000000000000, decimal_places=2)
image = models.URLField(null=True, blank=True)
category = models.CharField(max_length=64, choices=CATEGORY, default=None)
user = models.ForeignKey(User, on_delete=models.CASCADE, default="")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建HTML元素(例如复选框)或下拉选择
,然后使用
request
发送此选定的值,并在其是YES
的情况下将其与条件一起使用,然后Wishlist为true> TRUE ,反之亦然
you can create html element like check box or drop down selection
then send this selected value with
request
and use it with condition if itsYes
then wishlist isTrue
and vice versa