Saving Data in Multiple Selected Checkbox for Voting System

53 views
Skip to first unread message

Genaro Lubong

unread,
Feb 7, 2017, 4:34:31 PM2/7/17
to django...@googlegroups.com
Hello, newbie here, I am currently making our capstone project as a requirement to our course subject, and it is a major subject, while I was in the part of retreiving the selected candidates, I was not about to iterate the vote for those selected on the upper part, just the last selected item/object/candidate, and since it is the major objective of our project, and I am stucked with it and our team will be screwed if I didn't finished it As Soon As Possible, here are my codes:


#views.py

def vote(request):
     selected_candidate = Candidate.objects.get(pk=request.POST.get('candidate'))
     selected_candidate.totalvotes += 1
      selected_candidate.save()
      return redirect('vs:index')

#voting.html

{% if all_candidates %}
     <form action="{% url 'vs:vote' %}" method="post">
         {% csrf_token %}
         {% for organization in all_organizations %}
             {% if all_positions %}
                 <h1>{{ organization.organization }}</h1>
                 {% for position in all_positions %}
                     {% if organization == position.organization %}
                         <h3>{{ position.position }}</h3>
                         <div class="row">
                             {% for candidate in all_candidates %}
                                 {% if position == candidate.position %}
                                     <div class="column">
                                         <div class="col-md-4 col-sm-6 portfolio-item">
                                             <div class="portfolio-link" data-toggle="modal">
                                                 <div class="portfolio-hover">
                                                     <div class="portfolio-hover-content">
                                                             <input type="checkbox" id="candidate{{ forloop.counter }}" class="candidate{{ position.id }} fa fa-check fa-3x"  name="candidate" value="{{ candidate.id }}" />
                                                     </div>
                                                 </div>
                                                 <img src="{{ candidate.picture.url }}" class="img-responsive" />
                                             </div>
                                              <div class="portfolio-caption">
                                                 <h4>{{ candidate.studentid.firstname }} {{ candidate.studentid.middlename }} {{ candidate.studentid.lastname }} {{ candidate.studentid.suffix }}</h4>
                                                 <p class="text-muted">{{ candidate.position }} - {{ candidate.party }}</p>
                                             </div>
                                         </div>
                                     </div>
                                 {% endif %}
                             {% endfor %}
                         </div>
                     {% endif %}
                 {% endfor %}
             {% endif %}
         {% endfor %}
     <div class="intro-text">
             <input type="submit" value="VOTE" class="page-scroll btn btn-xl">
     </div>
      </form>
 {% else %}
     <h2>No Candidates Yet</h2> {% endif %}

#models.py

class Candidate(models.Model):
    studentid = models.OneToOneField(Student, on_delete=models.CASCADE)
    position = models.ForeignKey(Position, on_delete=models.CASCADE)
    party = models.ForeignKey(Party, on_delete=models.CASCADE)
    picture = models.ImageField()
    totalvotes = models.IntegerField(default=0)
    casvotes = models.IntegerField(default=0)
    citvotes = models.IntegerField(default=0)
    coevotes = models.IntegerField(default=0)
    ctevotes = models.IntegerField(default=0)


I hope, you can help me guys, thank you so much in advance.

Genaro Lubong

unread,
Feb 10, 2017, 9:50:20 AM2/10/17
to django...@googlegroups.com
please help me guys

ludovic coues

unread,
Feb 10, 2017, 11:14:39 AM2/10/17
to django...@googlegroups.com
You didn't provide the view making use of that template
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACY%3DutG8KLSZLYdpHpijg1QcY%3Dd0XP1DYoy8f-i_iNZwPPfi9g%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Genaro Lubong

unread,
Feb 14, 2017, 11:35:40 AM2/14/17
to django...@googlegroups.com

so I need to provide it? I am using def views for the template


> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACY%3DutG8KLSZLYdpHpijg1QcY%3Dd0XP1DYoy8f-i_iNZwPPfi9g%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ludovic coues

unread,
Feb 14, 2017, 1:49:03 PM2/14/17
to django...@googlegroups.com
You provided vote from views.py but vote redirect the user. It does
not use the provided voting.html template.

Also, in my opinion, they are way too many if and for. You have three,
nested, loops. And you are using only a subset of each loop data.
I assume position.organization is a foreign key. So you should be able
to do "{% for position in organization.position_set %}" instead of "{%
if all_position %}{% for position in all_position %}{% if organization
== position.organization %}"
Just the same, you should have a position.candidate_set, with the list
of all candidate in a position.

I hope that help.
These information come from the models.ForeignKey documentation [1]
which point to the queries documentation [2]

[1] https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_name
[2] https://docs.djangoproject.com/en/1.10/topics/db/queries/#backwards-related-objects
>> > email to django-users...@googlegroups.com.
>> > To post to this group, send email to django...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/django-users.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/django-users/CACY%3DutG8KLSZLYdpHpijg1QcY%3Dd0XP1DYoy8f-i_iNZwPPfi9g%40mail.gmail.com.
>> >
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>>
>> Cordialement, Coues Ludovic
>> +336 148 743 42
>>
>> --
>> 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...@googlegroups.com.
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACY%3DutHnaGi2rdJJBtHJ0SLm618QeFH-QhHqrJ7Kb4gpbv4QkQ%40mail.gmail.com.

Genaro Lubong

unread,
Feb 14, 2017, 7:00:38 PM2/14/17
to django...@googlegroups.com

thank you so much, it helped me a lot, gonna few more for further errors, I may encounter, thanks a lot


>> > To post to this group, send email to django...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/django-users.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/django-users/CACY%3DutG8KLSZLYdpHpijg1QcY%3Dd0XP1DYoy8f-i_iNZwPPfi9g%40mail.gmail.com.
>> >
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>>
>> Cordialement, Coues Ludovic
>> +336 148 743 42
>>
>> --
>> 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

>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAEuG%2BTY1FodJ6rdEVkwC8Qh8KHtKPngApjpGbjoK0MPvyevEOw%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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

> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CACY%3DutHnaGi2rdJJBtHJ0SLm618QeFH-QhHqrJ7Kb4gpbv4QkQ%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages