首页 > 解决方案 > 我的表单没有出现其他一些与出价系统 Django 问题有关的问题

问题描述

我想应用这个要求:

用户应该能够对项目进行投标。该出价必须大于出价,如果不显示错误消息

我试着做我所拥有的那件事

网址.py

urlpatterns = [
path('Post/<int:id>', views.viewList, name='viewList'),
# bids
path("Post/<int:id>/bid", views.take_bid, name="take_bid"),

# Close Bid
path('Post/<int:id>/close', views.closeListing, name="closeListing"),

]

视图.py

def viewList(request, id):
# check for the watchlist
listing = Post.objects.get(id=id)
if listing.watchers.filter(id=request.user.id).exists():
    is_watched = True
else:
    is_watched = False
context = {
    'listing': listing,
    'comment_form': CommentForm(),
    'comments': listing.get_comments.all(),
    'Bidform': BidForm(),
    'is_watched': is_watched
}
return render(request, 'auctions/item.html', context)
 @login_required 
 def take_bid(request, id):
       listing = Post.objects.get(id=id)
       bid = float(request.POST['bid'])
       if is_valid(bid, listing):
            listing.currentBid = bid
            form = BidForm(request.POST, request.FILES)
            newBid = form.save(commit=False)
            newBid.auction = listing
            newBid.user = request.user
            newBid.save()
            listing.save()
       return HttpResponseRedirect(reverse("viewList", args=[id]))
       else:
           return render(request, "auctions/item.html", {
               "listing": listing,
               "Bidform": BidForm(),
              "error_min_value": True
           })


   def is_valid(bid, listing):
           if bid >= listing.price and (listing.currentBid is None or bid > listing.currentBid):
               return True
           else:
               return False

模型.py

class Post(models.Model):
# data fields
title = models.CharField(max_length=64)
textarea = models.TextField()

# bid
price = models.FloatField(default=0)
currentBid = models.FloatField(blank=True, null=True)

imageurl = models.CharField(max_length=255, null=True, blank=True)
category = models.ForeignKey(
    Category, on_delete=models.CASCADE, default="No Category Yet!", null=True,  blank=True)

creator = models.ForeignKey(User, on_delete=models.PROTECT)

watchers = models.ManyToManyField(
    User, blank=True, related_name='favorite')
date = models.DateTimeField(auto_now_add=True)

# for activated the Category
activate = models.BooleanField(default=True)

buyer = models.ForeignKey(
    User, null=True, on_delete=models.CASCADE, related_name="auctions_Post_creator")

def __str__(self):
    return f"{self.title} | {self.textarea} |  {self.date.strftime('%B %d %Y')}"

 class Bid(models.Model):
          auction = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="item_id")
          user = models.ForeignKey(User, on_delete=models.CASCADE)
          bid = models.FloatField()
          bidWon = models.BooleanField(default=False)

我的 HTML 文件 (item.html)

                <div class="card-body">
                <ul class="list-group">
                    <li class="list-group-item mb-2">Description:<br>{{listing.textarea}}</li>
                    <li class="list-group-item mb-2">Price: {{listing.price}}</li>

                    <li class="list-group-item mb-2">
                        {% if listing.currentBid is None %}
                            {% if listing.creator != user  %}
                                <p class="text-muted">Start the first Bid!</p>
                            {% endif %}
                        {% elif listing.buyer is not None %}
                            {% if listing.creator == user %}
                                You've sold this item to {{listing.buyer}} for {{ listing.currentBid }}
                            {% elif listing.buyer == user %}
                                You've won this auction!
                            {% else %}
                            Current price: {{ listing.currentBid }}
                            {% endif %}
                        {% endif %}
                    </li>

                        {% if error_min_value %}
                            {% if listing.currentBid %}
                                <div class="alert alert-warning" role="alert">Your bid must be bigger than {{ listing.currentBid }}</div>
                            {% else %}
                                <div class="alert alert-warning" role="alert">Your bid must be equal or bigger than {{ listing.currentBid}}</div>
                            {% endif %}
                        {% endif %}

                        {% if listing.flActive and listing.creator != user %}
                        <div class="form-group">
                            <form action="{% url 'take_bid' listing.id %}" method="post">
                                <small class="text-muted">Edit Your Crunnt Bid:</small>
                                {% csrf_token %}
                                {{ Bidform }}        
                                <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                                    <input type="submit" value="Edit" class="btn btn-primary btn-sm m-1"/>
                                </div>
                            </form>                    
                        </div>         
                        {% endif %}

                    </li>
                        <p>category: {{listing.category.name}}</p>
                </ul>
            </div>

标签: pythonhtmldjangodjango-modelshalting-problem

解决方案


推荐阅读