django - Looping checkbox not all values adding to database -


I have the following function, what should it do, which should loop the submitted values ​​in the checkbox named Group, then Each value is in the database. However it seems to add a value to the database, is there a mess?

Groups = 1,3,3,4

Function Renewed

  def Add_batch (requested): # If we find a post, then get request post value. If we have valid data before saving to request.method == 'POST': form = batchform (request.POST) # to check if form.is_valid (): # Clear all data and add to var data. Data = form.cleaned_data group = data ['group']. Batch = batch (content = data ['message'], group = group.bizzets.jet (pk = item), user = request.user) batch. Save () returns for items in groups (","): Redirect (batch.jet_send_conformation_page),   

Post Works:

  Groups 1, 3, 4  < / Pre> 

Form:

  form action = "{% url 'add_batch'%}" method = "post" square = "form-horizontal" Encrypt = "Multipart / Form-Data" & gt; {% For items in% groups} & lt; Label class = "groups" & gt; & Lt; Input type = "checkbox" name = "group" value = "{{item.id}}" /> {{Item.name}} & lt; Br / & gt; & Lt; / Labels & gt; {% Endfor%} & lt; / Form & gt;   

forms.py

  class batchform (forms.Form): groups = forms. Defragment (max_length = 100)     

. This is absolutely fine.

When you submit a form like this, the parameter may look like this:

 ? Groups = 1 and group = 3 and group = 4   

Definition of your form; On the other hand, defining groups as a CharField The code will be populated with the value derived from the request.GET ['group'] , which will receive only one value.

I think that you would be better if you defined groups like:

  CHOICES = ((0, '1'), (1 , '2'), (2, '3'),) Class MyForm (forms.Form): groups = forms.MultipleChoiceField (option = choice, label = "group", required = false)    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -