Why isn't the BooleanField changing value in Django models?

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?

What database are you using for this?

I believe SQLite? I say that because there is a file called db.sqlite3 somewhere in the files. This project wasn’t made by me from scratch, so I got part of the models already done and I’m trying to create new ones. I don’t know if this helps, but these are the (only) imports in models.py that already were there when I downloaded the project:

from django.contrib.auth.models import AbstractUser 
from django.db import models

I’m guessing that it’s some type of caching issue. I’m assuming you’re running this in a test environment using runserver (or runserver_plus), and when you say there are no error messages, you’re reporting that there are no errors being shown in your console window where you’re running runserver.

First thing I’d do is stop runserver and restart it, and then reverify that the field has not changed.

Beyond that, I’d run this in the debugger and inspect the objects as they are processed by the view. If you’re not familiar or comfortable with using the debugger, you could add some print statements within the view to verify that what’s happening is what you expect to see.

But at this point, my only suggestion is to walk through this process step-by-step and verify that everything is right at each statement.

1 Like