[Answered ]-Django model dropdown missing in html view

1๐Ÿ‘

โœ…

Try changing your view variable name to team_numbers and replacing your team-stats.html snippet with the following:

<form method="post" action="">
    <select name="teams">
    {% for team_number in team_numbers %}
        <option value="{{ team_number }}">Team Num: {{ team_number }}</option>
    {% endfor %}
    </select>
</form>

Then update your view to:

class TeamStatsView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'team-stats.html',
            {'team_numbers':Team.objects.values('team_number')})                                             
๐Ÿ‘คNick S.

1๐Ÿ‘

You can use choices=NUMBERS

NUMBERS = (
    ('1','1'),
    ('2','2'),
    ('3','3'),
    ('4','4')
)
class Team(models.Model):
    team_number = models.IntegerField(choices=NUMBERS )
    team_notes = models.CharField(max_length=150)
    event_id = models.ForeignKey(
        'Event', on_delete=models.CASCADE, unique=False)

    def __unicode__(self):
        return str(self.team_number)

    class Meta:
        db_table = 'teams'
        app_label = 'frcstats'
๐Ÿ‘คerajuan

0๐Ÿ‘

Your view variable is called team_number.
Try to change TeamStatsView into team_number:

<form method="post" action="">
  {% csrf_token %} {{ team_number }}
  <input type="submit" value="Submit" />
</form>
๐Ÿ‘คilse2005

Leave a comment