如何检查布尔菲尔德ina django modelform是否为true,并在列表下添加任何内容?

发布于 2025-02-01 20:19:45 字数 2059 浏览 1 评论 0原文

我正在尝试创建一个电子商务网站(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2025-02-08 20:19:45

您可以创建HTML元素(例如复选框)或下拉选择

<label>Wishlist:</label><select  id="choice" name="wish" ><option value="" selected>Choose option</option><option value="True">yes</option><option value="False">no</option></select>

,然后使用request发送此选定的值,并在其是YES的情况下将其与条件一起使用,然后Wishlist为true> TRUE ,反之亦然

you can create html element like check box or drop down selection

<label>Wishlist:</label><select  id="choice" name="wish" ><option value="" selected>Choose option</option><option value="True">yes</option><option value="False">no</option></select>

then send this selected value with request and use it with condition if its Yes then wishlist is True and vice versa

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文