0

I need to create a 100-cell table, and in each cell if the corresponding entity exists, display it's information, otherwise, display "Empty". How do I do that? The python program (Item has properties of "seqNumber" and "name"):

query = db.Query(Item)
items = query.fetch(100)
render(..., {'range100':range(100), 'items':items}, ...)

HTML:

<table>
<tr>
{% for i in range100 %}  <!-- for item in items (how?) -->
<td>
    {% if item.seqNumber == forloop.counter (how?) %}
      {{item.name}}
    {% else %}
      Empty
    {% endif %}
{% endfor %}
</tr>
</table>
Randy Tang
  • 4,283
  • 12
  • 54
  • 112
  • well, you need a mechanism to insert new items and so a handler that you link to from the "Insert Item" link. You should also add in the loop counter into the link so your handler knows where to insert the new item. – Paul Collingwood Dec 18 '12 at 09:50
  • Thanks Paul. Maybe I didn't put my question appropriately. My problem is that I don't know how to create the table in the HTML, not the "Insert item" thing. I've re-edited the question. – Randy Tang Dec 18 '12 at 10:15
  • ah. I see. Pass the "list" of items directly to your template, perhaps as a tuple (range_ID,item_data) then refer to it in your template as item[0],item[1]. The order you pass them in will be the order they are iterated around. If the second item in the tuple is missing then that's where you trigger the insert else. – Paul Collingwood Dec 18 '12 at 10:20

1 Answers1

0
query = db.Query(Item)
items = query.fetch(100)
l = []
for i in enumerate(range(99)):
    try:
        l.append((i,items[i].name))
    except:
        l.append((i,None))

render(stuff = l)

This is all untested and the try/except is no doubt not ideal, just easier to write code then in a comment to give you the general idea of how I'd approach this.

<table>
<tr>
{% for i in stuff %}  
<td>
      {{ i.0 }}<!-- ID -->
      {% if i.1 %}
      {{ i.1 }}<!-- value -->
    {% else %}<!-- if the value is none -->
      "VALUE NEEDED"
    {% endif %}
{% endfor %}
</tr>
</table>
Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • Thanks again. The "Item" is a model class which has a "name" as one of its properties. However, the template system didn't seem to accept the format i[1].name. The error message was: TemplateSyntaxError: Could not parse the remainder: '[1].name' from 'i[1].name' – Randy Tang Dec 18 '12 at 12:08
  • no, it would not work like that as .name stopped existing when it went into the tuple. Refer to it as i[1] only. – Paul Collingwood Dec 18 '12 at 12:10
  • Well, then I have to figure out another solution. – Randy Tang Dec 18 '12 at 12:13
  • no, it's still there, it's just in the tuple as i[1] I've edited the answer to include .name explicitly into the tuple istead of the whole item object – Paul Collingwood Dec 18 '12 at 12:15
  • No, still got the same error: TemplateSyntaxError: Could not parse the remainder: '[0]' from 'i[0]' – Randy Tang Dec 18 '12 at 12:26
  • try the edit, also check this question: http://stackoverflow.com/questions/271077/django-how-to-do-tuple-unpacking-in-a-template-for-loop – Paul Collingwood Dec 18 '12 at 12:33
  • Paul, thanks for the link. I solved the problem: it should be i.0 and i.1, instead of i[0] and i[1]. My headache is cured ;-) – Randy Tang Dec 18 '12 at 13:17
  • Great stuff. Next time I willonly answer when i have time to test my proposal :) – Paul Collingwood Dec 18 '12 at 13:19
  • Well, the solution did come from the link you provided. Thanks again. – Randy Tang Dec 18 '12 at 15:55