I’m working in an Auction website which let the user list an auction and close it afterwards. The auction models has a boolean field called is_closed that stores if the auction is closed or not. When the user clicks a “Close Auction” button, he will be lead to this view that only changes the value of this field (that is false by default) and then saves the changes. The auction poster is able to click a button in the auction page that leads to this view:
def close_auction(request, auction_id):
if request.method=="POST":
target_auction = Auction.objects.get(id=auction_id)
target_auction.is_closed=True
target_auction.save()
return HttpResponseRedirect(reverse('auction', kwargs={'auction_id':target_auction.id}))
The form the poster clicks has only the button, he or she doesn’t need to click in a checkbox or anything. This view always sets the value to True. However, nothing changes after I save the new value. When I go to the /admin page afterwards the is_closed field is still set as false. If this is illegal, why there is no error messages? This is the form in the html file:
{% if auction.owner.username == user.username%}
<p></p>
<form auction="{% url 'close_auction' auction.id %}" method="POST">
{% csrf_token %}
<div class="form-group">
<input class="btn btn-primary" id="close_bid_button" type="submit" value="Close Auction">
</div>
</form>
And this is the Auction model:
class Auction(models.Model):
title = models.CharField(max_length=64)
description = models.TextField(max_length=100, default="")
image_url = models.CharField(max_length=100, default="https://elitescreens.com/images/product_album/no_image.png")
category = models.ForeignKey(Category, on_delete=models.CASCADE, default="", related_name="auction_category")
bid = models.OneToOneField(Bid, on_delete=models.CASCADE, null=True)
creation_date = models.CharField(max_length=30, default="-----")
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, default="")
is_closed = models.BooleanField(default=False)
I read something about using a Django Form with a boolean field, but I couldn’t manage to figure out how to make this form alter the field in a model. Any tips?