2

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.

rakin235
  • 93
  • 8
  • is `instance` the same as `listing`? – Brian Destura Jul 08 '21 at 05:29
  • Yes, it is. I also tried with listing but it does the same. – rakin235 Jul 08 '21 at 05:37
  • The problem remains the same, still, it refreshes the page. Although I changed instance to listing. – rakin235 Jul 08 '21 at 05:42
  • Just want to make sure you have added the exact code here. Have you tried checking it from the database if the status is changing? And if you are caching pages in some way? – Brian Destura Jul 08 '21 at 05:43
  • 1
    You can use `ajax` for this little change, it will also avoid reloading the page for that simple change. [refer this](https://stackoverflow.com/questions/68124323/how-to-allow-only-a-certain-section-of-my-code-to-reload-in-django/68125875#68125875) – Rasheed kotoor Jul 08 '21 at 05:43

1 Answers1

1

You can do that using ajax. Simply call the javascript function on button click and request the django view using ajax.

 <div style="margin-top: 2%;">
            <form action="{% url 'status' list.id %}" method="POST">
                {% csrf_token %}
                <button onclick="DoSomething();" class="btn btn- 
                   danger">Close</button> // added onlick="DoSomething();"
             </form>
            
        </div>

 <script>
    function DoSomething(){
      data = 'some data'
      $.ajax({
                 type: 'POST',
                 url: 'getuser/',
                 data: data,
                 processData: false,
                 contentType: false,
                 success: function(json) {
                     alert(json);
                 }
             })
     }
 </script>

You can also achieve that by adding event handler to the button. checkout this great answer here It might help you more.

Nomi
  • 185
  • 2
  • 13