lundi 1 juin 2015

Get value from a Django checkbox

I have a template where I create different checkboxes with subitems, once the checkboxes are selected and submitted I want it to return the item that was checked with all the subitems. This is how it's currently working http://ift.tt/1Ky5A28 .This is what I currently have in my template:

{% for key, value in dict.items %}
<form action="/chosen/" method="POST">
{% csrf_token %}

<ul>
<label name="choice"><li><input type="checkbox" name="checkbox" value={{ key}}>{{key}}</li>
<ol type="A">
    {% for item in value %}
<li>{{item }}</li></label>
        {% endfor %}
</ol>

</ul>
        {% endfor %}
<input type="submit" name="submit" value="Choose Questions" >
</form>

And this is my views:

def chosen(request):
if "checkbox" in request.POST:
    message=str(request.POST.get("checkbox"))
else:
    message="Nothing choosen"


return HttpResponse(message)

Currently,the value={{key}} in the template is only returning the first word of the checked item,but the {{key}} next to it displays the whole item. How can I get it to display the whole item and all the subitems? Like if you chose what is 1+1?, you would get:

What is 1+1?

  • 1
  • 2
  • 3
  • 4

Without the checkbox next to the question.




Aucun commentaire:

Enregistrer un commentaire