0

I'm writing an app that should list all teams from a local sports league inside a select box from which users can choose their favourite, however when I open the app everything looks good except that the select field shows only blank options. I know it's querying the DB correctly because it shows all 18 options inside the select box but they all appear blank and I get no error whatsoever.

Here is my model:

class Team(models.Model):
    team_id = models.CharField(primary_key=True, max_length=3)
    city = models.CharField(max_length=50)
    name = models.CharField(max_length=50)

    class Meta:
        managed = False
        db_table = 'team'

    def __unicode__(self):
        return self.team_id

my view:

@verified_email_required()
def crearcuenta(request):
    equipos = Team.objects.all()
    form = CuentaForm()
    if request.method == "POST":
        form = CuentaForm(request.POST)
        if form.is_valid():
            cuenta = form.save(commit=False)
            cuenta.user = request.user
            cuenta.equipo_favorito = request.POST.get("equipo", "")
            cuenta.save()
        return HttpResponseRedirect("/dashboard/")
    context = ({
        "equipos": equipos,
        "form": form
    })
    return render(request, "teams/crearcuenta.html", context)

And lastly the html:

<div class="col-md-8 form-group mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
                        <label for="Equipo" class="col-md-3 control-label mdl-textfield__label">Correo</label>
                        <div>
                            <select id="Equipo" name="equipo" class="selectpicker form-control" data-live-search="true">
                                <option>Seleccione uno</option>
                                {% for equipo in equipos %}
                                    <option value="{{ equipos.team_id }}">{{ equipos.city }} {{ equipos.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

Thanks!

1
  • Are you working with AngularJS? Commented Mar 14, 2016 at 17:50

1 Answer 1

3

In your loop, you have

{% for equipo in equipos %}

Inside your loop, you want to access the attribute of an individual equipo, so you should have {{ equipo.team_id }} instead of {{ equipos.team_id }}.

{% for equipo in equipos %}
<option value="{{ equipo.team_id }}">{{ equipo.city }} {{ equipo.name }}</option>
{% endfor %}

Ideally, you would include equipo in your form, and let Django take care of rendering the form (e.g. {{ form.equipo }}). Then you wouldn't have to manually render or validate the field, which prevents errors.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alasdair, that was the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.