How to render Queryset in a django table template

I have a model, which is defined as shown, which acts on the query and gets a list of objects that should be placed in the corresponding cells of the table. Here is the relevant piece of code.

class Location(models.Model):
    x=models.IntegerField(null=True)
    y=models.IntegerField(null=True)
    z=models.CharField(max_length=5,null=True)

    def __unicode__(self):
        return self.z

From this db, I want to get all the objects and put them in a 2d table with the row and column defined by x, y of this object. If there is no specific object (x, y), then this particular interval should be displayed empty in the table. This is an opinion that I wrote to meet these goals.

def gettable(request):
    events=[]
    for xdim in xrange(3):
        xe=[]
        for ydim in xrange(3):
            object=[0]
            object.append(Location.objects.filter(x=xdim,y=ydim))
            xe.append(object[-1])
            events.append(xe)
    return render(request, 'scheduler/table.html', {'events':events})

Here is the html part of the code

<table border="1">
    <th>Header 0</th>
    <th>Header 1</th>
    <th>Header 2</th>
    {% for event in events %}
    <tr>
    {% for x in event %} <td>{{ x }}</td>
    {% endfor %}
    </tr>
    {% endfor %}
</table>

I need to solve a few problems here.

1. ( , , django ), , , (x, y ) .

2. [<Location: 21>], , "21".

3. , - (x, y).

4., , .

+5
3

, django-tables2. .

sais:

django-tables2 HTML . . HTML-, django.forms HTML-. .

:

  • , Django.
  • JavaScript.
  • Django.
  • .
  • .
  • .
  • HTML.
  • mixin Django 1.3.

, :

import django_tables2 as tables

class SimpleTable(tables.Table):
    class Meta:
        model = Simple 

:

def simple_list(request):
    queryset = Simple.objects.all()
    table = SimpleTable(queryset)
    return render_to_response("simple_list.html", {"table": table},
                              context_instance=RequestContext(request))

, , :

{% load django_tables2 %} 
{% render_table table %}

, django-tables2 ! .

.

+14

:

  • IMO .
  • __unicode__ ( __string__), .
  • , .

+1

2 , , {{x.0}} , , .

+1

All Articles