简体   繁体   中英

how can i use a variable from my view on a if statement in django

Im trying to change a boolean value in django when I click an input image I think I already done it but now when I try to compare this boolean in my html it doesnt bring the correct value it always brings its as False (sn6.jpg)

View.py

def reservacion(request):
if request.method == "POST":
    estatus = Asientos.objects.get(asiento=1)
    if estatus.status == True:
        estatus.status = False
        estatus.save()
    else:
        estatus.status = True
        estatus.save()
return render(request,"reservacion.html")

Models.py

class Reserva(models.Model):
mesa = models.CharField(max_length=2,primary_key=True)
asiento = models.ForeignKey(Asientos)
def __str__(self):
    return self.mesa

html-- in here my if statements change the value of the boolean on my DB but it always shows the image in False (sn6.jpg)

<form enctype="multipart/form-data"  method="post">
{% csrf_token %}
<div class="row">
{% if estatus.status == True%}
<div class="col"><input class="d-block w-100" type="image" src="{%static "/img/s6.jpg" %}" name="1" value=""></div>
{% else %}
<div class="col"><input class="d-block w-100" type="image" src="{%static "/img/sn6.jpg" %}" name="1" value=""></div>
{% endif %}
</form>

Am I missing something?, how can i get the other oimage when i click and submit the value?

Two points:

  • you need to add a context dictionary to your render to pass variable to your template : render(request,"reservacion.html", {'estatus': estatus})
  • and no need to compare a Boolean to True , this is enough : if estatus.status

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