Italo Lemos
Italo Lemos

Reputation: 1022

How to pass checkbox values to view

I'm coding a project to learn django. I have one checkbox that needs to change a value from the model. I'll try to summarize how my project is

# Model
class Model(models.Model):
  is_confirm = models.BooleanField(default=True)

# template
<div>
   <input type="checkbox" id="confirm" checked="{{ model.is_confirm }}">
</div>

<button class="btn btn-info" data-toggle="modal" data-target="#updating" style="width:100%">
 <span class="glyphicon glyphicon-floppy-disk"></span>Save
</button>

<div class="modal-header">
    <form action="{ url 'confim_view' model.id" method="post">{% csrf_token %}
       <button type="submit" class="btn btn-success" type="submit" value="Update">Confirm</button>
    </form>                                         
</div>

Basically, I have the checkbox and I want update its value when a click the button save, a modal is open and when I click in Confirm button the value on database is changed. Actually, I can't get the value of the checkbox and send it to view via POST method. Does anyone have any idea how I can make this work?

Obs: The view to save is other than the view to show the object itself

Upvotes: 0

Views: 1719

Answers (1)

Thyrst
Thyrst

Reputation: 2568

If I understand you correctly, you want to to this:

template.html:

<input type="checkbox" id="confirm" {% if model_is_confirm %} checked="checked" {% endif %}>

view.py:

obj = Obj.objects.get(id=obj_id)
return render(request, 'template.html', {'model_is_confirm': obj.is_confirm})

Upvotes: 2

Related Questions