On Google app engine, I’d like to use two variables in a template for loop: I need to input, say, 10 items. If there are already some items in the datastore, then show them, otherwise, leave the field blank. How do I do it? Thanks.
BTW, I noticed that there is “multifor” in Django, but I don’t know how to install it on GAE.
main.py:
def mainPage(webapp2.RequestHandler):
query = db.Query(Item)
items = query.fetch(limit=10)
template_values = {'range': range(10), 'items': items}
common.render(handler, 'main.html', template_values)
main.html:
<form action=... method="post">
{% for i in range; for item in items %}
<input type="text" name="name" value="item.name">
{% endfor %}
<input type="submit">
</form>
Not sure which templating engine you are using, but what about creating a list of 10-elements and filling it with the results of your query? Something like:
Then, once you get your query results, add them to the list (the objects will be returned as a list of objects, so this is just a conceptual example):
Now when you pass
my_listinto the template: