On the user end, say I have people selecting school records through a form to display in a table below.
For instance, they choose their name from a list of names, and that pulls their records from a database.
Dependent on their class year, they may have 1, 2, 3, or 4 years of records, so the data pulled will always look different.
After they submit the form, their records are stored in a variable containing all their records, and then each record is broken down into subtypes, for instance, all records in the English department. Those subtypes are stored in other lists--so there's a list for all English records. Let's call that variable english_records. I want to use these subtype variables to be able to present only the data users want to see, and to present all data in that particular list.
So using Flask's render_template function, I'm trying to send each of these records to an html template that will create a table cell for each record.
What I've been trying (that hasn't worked so far) is something like:
Python:
i = 0
def index():
for e in english_records:
english_records = [
{
'english': english_records[i]
}
]
i = i + 1
return render_template("index.html",
english_records = english_records)
And in HTML:
...table above...
{% for record in english_records %}
{{record.english}}
{% endfor %}
...table continues...
Thus far I've been getting table cells created for each record, but the records not being passed through. Anyone know how to do this?
解决方案
Is there a reason why you're pre-processing the data? What does english_records look like? If my hunch is correct, then you shouldn't actually need the for loop at all.
There's also the issue that you're overwriting the initial english_records variable with english_records within the for loop, so the assignment, while legal in terms of syntax, is logically nonsensical.
Another issue is that, depending on the actual type of the first english_records, you shouldn't need to use a counter: if english_records is a list, then it'll contain the value you're looking for. If english_records is a dict, then enumerating it might look like for key, val in english_records.iteritems().