dimanche 22 novembre 2015

Cannot add data in database using checkbox Django

Hello guys I need you help! I am trying to add data using checkboxes in my template. I got problem saving the data from checkboxes(in template). I use loop in my views.

this is my model:

class Vote(models.Model):
candidate_id = models.ForeignKey('Candidate', blank=True, null=True)
election_id = models.ForeignKey('Election', blank=True, null=True)
user_id = models.ForeignKey('User', blank=True, null=True)

def __str__(self):
    return "%s %s %s" % (self.user_id, self.election_id, self.candidate_id)

in my template:

<form method="POST" class="form-horizontal" role="form">

                              {% csrf_token %}
                                  <div class="form-group ">
                                  {% for position in positions %}
                                      <label for="cname" class="control-label col-lg-2">{{ position }}<span class="required">*</span></label>

                                      {% for candidate in candidates %}
                                        {% if position.pk == candidate.position_id.pk %}
                                          <div class="col-lg-3">
                                              <input type="checkbox" name="candidate_id" value="{{ candidate.pk }}">{{ candidate }}<br>
                                          </div>
                                        {% endif %}
                                      {% endfor %}

                                  </div>
                                  {% endfor %}
                                          <button class="btn btn-primary" type="submit">Save</button>
                                          <button class="btn btn-default" type="button">Cancel</button>
                                      </div>
                                  </div>
                              </form>

views.py:

def vote(request):

if request.user.is_authenticated and request.user.is_admin:
    candidates = Candidate.objects.all()
    election = Election.objects.all().filter(is_active=True)
    positions = Position.objects.all()
    user = get_object_or_404(User, pk=request.user.pk)

    try:
        if request.method == 'POST':
            candidate_id_list = request.POST.getlist('candidate_id')
            counter = 0

            for candidate_list in candidate_id_list:
                candidate_pk = candidate_id_list[counter]
                election = Election.objects.all().filter(is_active=True)
                candidate = get_object_or_404(Candidate, pk=candidate_pk)
                user = get_object_or_404(User, pk=request.user.pk)
                vote = Vote(candidate_id=candidate, election_id=election, user_id=user)
                vote.save()

            return redirect('system.views.user_home')

        else:
            form = VoteForm()
            return render(request,'system/vote.html', {'form':form, 'candidates': candidates,
                                                        'election': election, 'user': user,
                                                        'positions': positions})

    except:
        exist = "ERROR!"
        form = VoteForm()
        return render(request,'system/vote.html', {'form':form, 'exist': exist})

elif not request.user.is_authenticated:
    return redirect('system.views.user_login')

only the candidate_id is save in the database. What's wrong in my codes? Thanks.




Aucun commentaire:

Enregistrer un commentaire