views:

337

answers:

2

I wonna simple tag, for showing table of any model arrays like:

{% table city_list %}

Do anybody see such things?

+1  A: 

You can try django-tables app, which allows you to do the following, given model Book:

# Define 
class BookTable(tables.ModelTable):  
  id = tables.Column(sortable=False, visible=False)  
  book_name = tables.Column(name='title')  
  author = tables.Column(data='author__name')  
  class Meta:  
      model = Book  

# In your views
initial_queryset = Book.objects.all()  
books = BookTable(initial_queryset)
return render_to_response('table.html', {'table': books})  


# In your template table.html
<table>  

<!-- Table header -->
<tr>  
   {% for column in table.columns %}  
   <th>{{ column }}</th>
   {% endfor %}
</tr>  

<!-- Table rows -->
{% for row in table.rows %}
<tr>
   {% for value in row %}
   <td>{{ value }}</td>
   {% endfor %}
</tr>
{% endfor %}

 </table>

I think the above is much more elegant and self explanatory than just doing {% table book_list %}

drozzy
The code is big, and make the same standart code. I like when all standart things works automaticly, unless I decided to change some part.
dynback.com
In that case try asp.net
drozzy
A: 

Try generic vieww e.g. http://www.djangobook.com/en/2.0/chapter11/

gamesbook
very smart) ....
dynback.com

related questions