如何访问Django表格的提交值?

发布于 2025-02-01 04:27:50 字数 1668 浏览 2 评论 0原文

我正在尝试建立一个电子商务网站,以使用户可以通过表格订立出价。用户帖子必须大于上市价格和任何其他出价的新出价。我需要帮助访问用户提交的价值以检查它是否符合先前所指定的要求。

views.py

def listing(request, id):
    #gets listing
    listing = Listings.objects.get(id=id)
    #code for forms
    listing_price = listing.bid
    comment_obj = Comments.objects.filter(listing=listing)
    form = CommentForm()
    bid_form = BidsForm()
    if request.method == "POST":
        form = CommentForm(request.POST)
        bid_form = BidsForm(request.POST)
        new_bid = bid_form.cleaned_data.get('newBid')
        if form.is_valid():
            comment = form.save(commit=False)
            comment.listing = listing
            comment.user = request.user
            comment.save()
        if (bid_form.is_valid()) and (new_bid >= listing_price):
            bid = form.save(commit=False)
            bid.listing = listing
            bid.user = request.user
            bid.save()
        else:
            return render(request, "auctions/listing.html",{
               "auction_listing": listing,
               "form": form,
               "comments": comment_obj,
               "bidForm": bid_form
               })
    return render(request, "auctions/listing.html",{
        "auction_listing": listing,
        "form": form,
        "comments": comment_obj,
        "bidForm": bid_form
    })

(有两种形式,一种用于评论,另一种用于投标。)

html

<!--bid form-->
    <form action = "{% url 'listing' auction_listing.id %}" method = "POST" name = "newBid">
        {% csrf_token %}
        {{ bidForm }}
        <input type = "submit" value = "Place Bid">
    </form>

I am trying to make an e-commerce site that allows the user to place bids through a form. The new bid that the user posts has to be larger than the listing price on the listing and any other bids. I need help accessing the value that the user submits to check if it meets the previously stated requirements.

views.py

def listing(request, id):
    #gets listing
    listing = Listings.objects.get(id=id)
    #code for forms
    listing_price = listing.bid
    comment_obj = Comments.objects.filter(listing=listing)
    form = CommentForm()
    bid_form = BidsForm()
    if request.method == "POST":
        form = CommentForm(request.POST)
        bid_form = BidsForm(request.POST)
        new_bid = bid_form.cleaned_data.get('newBid')
        if form.is_valid():
            comment = form.save(commit=False)
            comment.listing = listing
            comment.user = request.user
            comment.save()
        if (bid_form.is_valid()) and (new_bid >= listing_price):
            bid = form.save(commit=False)
            bid.listing = listing
            bid.user = request.user
            bid.save()
        else:
            return render(request, "auctions/listing.html",{
               "auction_listing": listing,
               "form": form,
               "comments": comment_obj,
               "bidForm": bid_form
               })
    return render(request, "auctions/listing.html",{
        "auction_listing": listing,
        "form": form,
        "comments": comment_obj,
        "bidForm": bid_form
    })

(There are two forms, one for comments and one for bids.)

html

<!--bid form-->
    <form action = "{% url 'listing' auction_listing.id %}" method = "POST" name = "newBid">
        {% csrf_token %}
        {{ bidForm }}
        <input type = "submit" value = "Place Bid">
    </form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

花辞树 2025-02-08 04:27:50

我能够通过添加以下代码来访问上市价格的价值:
new_bid = bid_form.cleaned_data.get(“ bid”)之后如果bid_form.is_valid()::并创建了一个新if语句以检查new_bid是否比listing_price_price_price_price_price_price_price_price 。我还删除了If request.method ==“ post”:,这引起了错误。

I was able to access the value of the listing price by adding this line of code:
new_bid = bid_form.cleaned_data.get("bid") after if bid_form.is_valid(): and created a new if statement to check if the new_bid is larger than the listing_price. I also removed if request.method == "POST":, which was causing an error.

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