0

I'm trying to create a django template generated table.

How do I do this:

<div id="holder" onclick="dostuff()">
    <table id="table" bgcolor="{{s.color_hash}}">
        <tbody>
            {& for x in s.num_rows &}
                <tr id="r">
                    {& for y in s.num_columns &}
                        <td id="c" height='{{s.height}}' width='{{s.width}}'></td>
                    {& endfor &}
                </tr>
            {& endfor &}
        </tbody>
    </table>
</div>

Where the <tr id="r"> I would like to have the number from x in {& for x in s.num_rows &}.

I'd like the id to be that of the value from the for loops

Any clue?

Thank you

zsquare
  • 9,916
  • 6
  • 53
  • 87
user3043594
  • 166
  • 10

1 Answers1

0

Like Burhan Khalid said use {{ x }}. But iterating in templates doesn't work this way. "Take a look at these template filters and tags, either of which is easy enough to include in your application.

http://www.djangosnippets.org/snippets/1357/

The advantage of these compared to the other solutions (passing in a range of numbers) is that, once installed, these will always be available to your templates and template authors, without having to explicitly pass a valid range through your view code."

See Numeric for loop in Django templates. Once you have the "get_range" filter you could write your code something like this:

<div id="holder" onclick="dostuff()">
    <table id="table" bgcolor="{{s.color_hash}}">
        <tbody>
            {% for x in get_range(s.num_rows) %}
                <tr id="{{ x }}">
                    {% for y in get_range(s.num_columns) %}
                        <td id="c" height='{{s.height}}' width='{{s.width}}'></td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
Community
  • 1
  • 1
fragles
  • 680
  • 6
  • 19