Моя форма не отображается с другой Некоторые проблемы с системой заявок Проблемы с Django
Я хочу применить это требование:
пользователь должен иметь возможность сделать ставку на товар. эта ставка должна быть больше, чем предложение, и если это не так, покажите сообщение об ошибке
.
моя проблема в том, что когда я запускаю сервер, форма заявки исчезает и я не вижу ничего, кроме этого параграфа
.<p class="text-muted">Start the first Bid!</p>
Я стараюсь делать то, что у меня есть
urls.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"),
]
views.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
models.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>