If your data is in the 'data' context variable, then try this:

        <table>
        {% for d in data %}
            {% if forloop.counter0|divisibleby:"2" %}<tr>{% endif %}
            <td>{{d}}</td>
            {% if forloop.counter|divisibleby:"2" %}</tr>{% endif %}
        {% endfor %}
        {% if data|length|divisibleby:"2" %}{% else 
%}<td>&nbsp;</td></tr>{% endif %}
        </table>

It's a little awkward, and doesn't generalize to more than two columns, 
but it does produce properly structured HTML.

If you're willing to add some code to your view, you can get more columns:

In the view:

    context['data'] = data
    ncols = 3
    for i, d in enumerate(data):
        d.firstcol = (i % ncols == 0)
        d.lastcol = (i % ncols == ncols-1)
    context['data_blanks'] = [None]*((ncols-len(data)%ncols)%ncols)

In the template:

        <table>
        {% for d in data %}
            {% if d.firstcol %}<tr>{% endif %}
            <td>{{d}}</td>
            {% if d.lastcol %}</tr>{% endif %}
        {% endfor %}
        {% for blank in data_blanks %}<td>&nbsp;</td>{% endfor %}
        {% if data_blanks %}</tr>{% endif %}
        </table>

--Ned.
http://nedbatchelder.com

andrej kesely wrote:
> hi,
> i have small question:
> suppose i have data in QuerySet - ['A', 'B', 'C', 'D', 'E', 'F',
> 'G'].
> I want make from this set two-column table in my template:
>
>   A    B
>   C    D
>   E    F
>   G
>
> What is the fastest way to do it? I don't want split the QuerySet in
> my views to something like this: [(A, B), (C, D)...] etc.
>
> Thanks,
> Andrej Kesely
>
> >
>
>   

-- 
Ned Batchelder, http://nedbatchelder.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to