I'm working on a Django project. My motive is to update the value of status in the database by simply clicking on a button. I don't want to submit any form. This is what I've tried:
Models.py
class auction_listing(models.Model):
title = models.CharField(max_length=100)
status = models.BooleanField(default=True)
def __str__(self):
return f"{self.id} : {self.title} "
Here status is the boolean field. By default it's true. No form required here but I want this status is to be changed with a button click. Here is the button in my HTML template.
template.html
<div style="margin-top: 2%;">
<form action="{% url 'status' list.id %}" method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Close</button>
</form>
</div>
Here is my views.py function:
views.py
def status(request,list_id):
listing = auction_listing.objects.get(pk=list_id)
if request.method == "POST" and listing.status is True:
listing.status = False
listing.save()
else:
listing.status = True
listing.save()
return HttpResponseRedirect(reverse("displaylistitem",kwargs={"list_id":list_id}))
urls.py
path("<int:list_id>",views.display_list,name="displaylistitem"),
path("<int:list_id>", views.status,name="status"),
This is all I tried. The problem is when I press the button, it simply refreshes the page rather than updating the value of status in database.