r/learnpython • u/A_Hot_MessTM • Aug 24 '26
Carry value from Python for-loop to HTML for-loop
I am currently working on a website (using Flask, SQLAlchemy and Jinja).
Context: I am making a ttrpg initiative tracker. For multiple fights to exist and be able to have multiples of the same creature and reuse creatures, the way I am formatting it is the user creates the base creature, and its row is duplicated, holding the id for the fight itself as fight_spec and the id of the base creature as og_id. I am currently working on making it so the user can edit the count of any creature in a fight.
For the page where the editing happens I want it to display the current count as the default value, but I cannot for the life of me figure out how to make sure it goes from python's for loop to html. My attempts typically end up with the value being set to "count_<id>" rather than the actual number that my count command generates. How should I go about this?
I have my current code below, but if there is a different way I should go about this, I'm open to hearing! Thank you <3
Python
unique_combatants = db.session.scalars(stmt).all()
for x in unique_combatants_set:
'count_' + str(x) = Combatant.query.filter_by(og_id=x, fight_spec=id).count()HTML
{% for combatant in unique_current_combatants %}
{{ combatant.combatant_name }}
<input type="number" name="combatant_count_{{combatant.id}}" id="combatant_count_{{combatant.id}}" value="count_{{combatant.id}}">
{% endfor %}