Saturday, June 1, 2013

save data using checkbox click an input

Hi,i am using django developing an app.Problem i am facing is i queried a list of data from one table and save the same data to another table,but the condition is if the user clicks the checkbox and press save.If the checkbox is clicked the value(label) of the checkbox should save in database,this is my requirement in user level(i explained).I tried with the below code ,

views.py

def what(request):
    typeList = Types.objects.filter(user=user, is_active=True,parent_type_id=None)
    list = []
    for type in typeList:
        if not type.parent_type_id:
            list.append(type)
            subtype = Types.objects.filter(parent_type_id=type.id, is_active=True)
            for subtypes in subtype:
                list.append(subtypes)
    if request.method == 'POST':
        ReportType.objects.filter(report=report).delete()
        checked_ones = [unicode(x) for x in subtype if unicode(x) in request.POST.keys()]
    for entry in checked_ones:
            r = ReportType()
            r.report = report
            r.title = entry
            r.save()
        return redirect('/member/where/')
    checked_ones = [x.title for x in ReportType.objects.filter(report=report)] 
    return render(request, 'incident/what.html',{''typeList':list,'})

template is
<form  method="post" action=".">
 {% csrf_token %}
{% for type in typeList%}
     {% if type.parent_type_id == None %}
       <h1>{{type.title}}</h1>
       {% else %}
       <p><input type="checkbox">{{type.title}}</input></p>
      {% endif %}
{% endfor %}
</form>

See,i posted all my code to better understand.

Let me tell you the problem i am facing.

1.I am not able to save the data to database,i believe that my view is ok,might be some problem with template.

Please help me in do this.

Thanks
   

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment