mardi 4 août 2015

How to submit ids of model's objects using checkboxes?

I have a model:

class Lesson(Product):
    title = models.CharField(max_length=200)

Now I want to create a form that will display my objects like so:

  • Object1 (check box)
  • Object2 (check box)
  • Object3 (check box)

when you select check boxes form has to submit ids of selected check boxes to a view.

  • Object1 (✓)
  • Object2 (☐)
  • Object3 (✓ )

have to result [1, 3] to be submitted

I've done this in plain HTML:

<div class="container">
  <div class="row mt-30">
    <div class="col s12 offset-m1 m10">
      <form action="#" method="get">
        <ul class="collection with-header">
          <li class="collection-header">
            <h4>{{lesson.title}}</h4>
          </li>
          {% for pk, stitle in objs %}
          <li class="collection-item">
            <div><input class="" type="checkbox" id="box{{ pk }}" value="{{ pk }}" />
            <label class='blue-grey-text text-darken-3' for="box{{ pk }}">{{ stitle }}</label></div>
          </li>
          {% endfor %}
        </ul>
        <button class="btn waves-effect waves-light" type="submit" name="action">
          Start the Lesson 
        </button>
      </form>  
    </div>
  </div>
</div>

But I have no ideas how to do this with Django's forms.

What is the way organize this behavior?




Aucun commentaire:

Enregistrer un commentaire