django iterating over the querydict

stack link
https://stackoverflow.com/questions/39565023/django-querydict-only-returns-the-last-value-of-a-list

view file

def save_state(request):
    print '##from SAVESTATE view##'    print request.POST
    print '#######################'    if request.method =='POST':
        for item in request.POST.getlist("task[]"):
            todo = Todo(title=item,completed=True,author=request.session.get('uname'))
            todo.save()
        print "##############"    return HttpResponseRedirect('/todos')



Template file

<form method="post" action="{% url 'save_state'%}">
{% csrf_token %}
<!--list task list items -->{% for todo in todos %}

<input type="checkbox" name="task[]" value="{{ todo.title }}"        {% if todo.completed is True %}
            checked = "checked"        {% endif %} ><label>{{ todo.title }} : {{ todo.author }} </label><br>
{% endfor %}

 <input type="submit" name="submit" value="Submit">
</form>

No comments:

Post a Comment