简体   繁体   中英

how can I change the value of a booleanfield from static file using checkboxes

I've just started using django and I'm not so familiar with it. This is my problem, I simply want to tick a checkbox and change the value of a booleanfield in the model which is default=false to true

I'm trying to create a function from views.py, but if there is any way to do it directly from the static file where my checkbox is that would do the work for me either

This is my views.py

def check(request):
    check = AddToCart.objects.get(orderd = request.GET['orderd'])
    check.is_active = True
    check.save()

This is the checkbox

Check: <input type="checkbox" name="checked" value="checked">

This is my model on models.py:

class AddToCart(models.Model):
    id = models.AutoField(primary_key=True, unique=True)
    id_product = models.ForeignKey(Produkte, on_delete = models.DO_NOTHING)
    quantity = models.IntegerField()
    id_user = models.ForeignKey(User, on_delete = models.DO_NOTHING)
    orderd = models.BooleanField(default=False)

   @property
   def cmimi_total(self):
       return self.quantity * self.id_product.cmimi

I have some objects inside AddToCart and I want to change the boolean values of each of them to True when the checkbox is ticked. I know my problem is just a basic one but as a newbie i could use some help. Thank You !

I removed the checkbox and simply used a button to turn the boolean values in the model to True this is my views.py

def check_out(request):
if request.method == 'GET':
    checked = AddToCart.objects.filter(id_user = request.user, orderd = False)
    for a in checked:
        a.orderd = True
        a.save()
    return HttpResponseRedirect('/browse/')

and the button into html file

<a type="button" href="/check/" class="btn btn-ge style-4 btn-warning btn-sm" id="buy_button">Buy Now</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM