Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django templates iterate through list of tuples and give them all buttons

I have a User model and the user's have university attribute. I want users to be able to scroll through a list of universities and choose one as a button, I don't want to have a dropdown selecter thing. So I passed in the UserInfo.UNIVERSITY_CHOICES (UserInfo is a one to one with the user model) to my template and try to iterate through it using:

<form method="get" action="/newUniversity/">
{% csrf_token %}

    {% for school in universityList %}
         <input class='submitbtn' type="submit" name="school" value="{{ school }}"></center>
    {% endfor %}
</form>

However I get a bunch of buttons that say ('Harvard','Harvard') instead of just Harvard. I tried to index the tuple by doing school[1] but that gives me an error. Is there a way to do this or am I doing it a completely wrong way?

like image 622
Chase Roberts Avatar asked Dec 18 '25 04:12

Chase Roberts


1 Answers

If UserInfo.UNIVERSITY_CHOICES is tuple (like the ones you would use with the django admin) you must choose which of the values you are after:

school.0

or

school.1

Notice Django's Template Language is not Python! You can see a reference of the language here: https://docs.djangoproject.com/en/dev/topics/templates/

like image 63
Nathan Avatar answered Dec 19 '25 22:12

Nathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!