0

Following along the Django polls app tutorial, I was wondering if instead of having a Charfield for the choice Model and manually adding every response/choice to the database; Is it possible to have choices?

For example:

class Poll(models.Model):
    text = models.CharField(max_length=255)
    pub_date = models.DateField()

def __str__(self):
    return self.text

class Choice(models.Model):
    question = models.ForeignKey(Poll, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=255)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return "{} - {}".format(self.question.text[:25], 
         self.choice_text[:25])

You have standard choices for every Poll like this:

class Poll(models.Model):
   text = models.CharField(max_length=255)
   pub_date = models.DateField()

   def __str__(self):
      return self.text



class Choice(models.Model):
    VOTING_CHOICES = (
    ('Aye', 'Aye'),
    ('Nay', 'Nay'),
    ('Abstain', 'Abstain'),
    )
    question = models.ForeignKey(Poll, on_delete=models.CASCADE)
    choice_text = models.CharField(
                  max_length=7,
                  choices=VOTING_CHOICES,
                  default='Aye',
                  )**
    votes = models.IntegerField(default=0)

     def __str__(self):
        return "{} - {}".format(self.question.text[:25], 
         self.choice_text[:25])


Poll Detail page
========


{{ poll }}
<form action="" method="post">
    {% csrf_token %}

    {% for choice in poll.choice_set.all %}
    {% for i,k in choice.VOTING_CHOICES %}

      <input type="radio"
          name="option"
          id="choice{{forloop.counter}}"
          value="{{ i }}"/>
      <label for="choice{{forloop.counter}}">{{ k }}</label>
     {% endfor %}
     {% endfor %}

    <input type="submit" value="Vote">

    </form>

views.py

def poll_detail(request, poll_id):

    #render poll detail page

    poll= get_object_or_404(Poll, id=poll_id)

    if request.method =="POST":
        print(request.POST)
     # Debug to see what data I am posting on form submission 

    context = {
        'poll':poll,
    }

    return render(request, 'app/poll_detail.html', context)

Does that make sense? Every time I try to implement this, I either get an empty dictionary response when I POST from the form or the options show up in tuples(or do not render at all).

Ernest A
  • 1
  • 2
  • Could you share your html template and your `views.py`? – Nguyễn Vũ Thiên May 07 '21 at 01:04
  • Wow, surprised by the quick response. I edited the question to include the html code. The three choices display, but I have to go into the admin panel to add a choice to the model first beforehand. Is there a way to get all the choices for each poll? – Ernest A May 07 '21 at 01:39
  • If you want create multiple checkbox choices for poll, you can see this post https://stackoverflow.com/questions/2726476/django-multiple-choice-field-checkbox-select-multiple – Nguyễn Vũ Thiên May 07 '21 at 02:10
  • I will look at the kin thanks; however I don't want multiple checkboxes. I want to present three options: Yes, No, Abstain on the polls. I have figured out how to post the selection, but I wanted to have every poll initially have those three options without adding choices manually. – Ernest A May 09 '21 at 13:53

0 Answers0