checkbox - Django - Iterating Checkboxes from ManyToManyField then adding in Views -


i'm making program let users create lists out of of contacts have available. i'd have entire list iterate checkboxes, in views, add checked boxes contact list's contacts list in 1 go.

i know how iterate checkboxes, don't know how tell views check boxes see ones checked, add associated user list.

in models.py:

class organization(models.model):     users = models.manytomanyfield(user, related_name='organizations')  class contact(models.model):     unique_id = models.integerfield(default=1)     email_address = models.charfield(max_length=50, null=true, blank=true)     name = models.charfield(max_length=200, null=true, blank=true)  class contactlist(models.model):     unique_id = models.integerfield(default=1)     name = models.charfield(max_length=200, null=true, blank=true)     contacts = models.manytomanyfield(contact, related_name='contact_lists') 

in contactlist.html:

<form action="/contactlists/add_contacts/ method="post">{% csrf_token %}     {% contact in contacts %}         <p><input type="checkbox" name="contacts" value="{{contact.unique_id}}">{{contact.name}}</p>     {% endfor %} <input type="submit" name="submit" value="new contact list"> 

in views.py:

def add_contacts(request, organization_id=1):     organization = organization.objects.get(id=organization_id)     contacts = contact.objects.all()     if request.method == 'post':         contacts_to_add = request.post.getlist('contacts')         contact in contacts_to_add:             if contact.contacts:                 contact = contact.objects.filter(unique_id=contact.unique_id)[0]                 contactlist.contacts.add(contact)     args = {'contacts': contacts, 'organization': organization}     args.update(csrf(request))     return render(request, 'contactlist.html', args) 


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -