I had a difficult time understanding how to delete an item from a table within a template, using a modelform. I couldn't find a good example, so I wanted to post the code that ultimately worked.
Render a given instance of collections.Counter into a 2 column html table.
Optionally accepts `column_title` keyword argument which sets the table
key column header.
Usage:
{% counter_table event_counter column_title='event type' %}
The above will render the a table from the `event_counter` variable with the first (key) column set to "event type".
See below for an example template (i.e `counter_table.html`)
{% load i18n %}
<table>
<thead>
<tr>
<th>{{column_title|capfirst}}</th>
<th>{% trans "count"|capfirst %}</th>
</tr>
</thead>
<tbody>
{% for key, count in most_common %}
<tr>
<td>{{key}}</td>
<td>{{count}}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td>{% trans "total"|capfirst %}</td>
<td>{{total_count}}</td>
</tr>
</tfoot>
</table>
added commands:
altersql - shows sql code with alter queries
alterdb - apply alter queries. parameters:
--showsql - show queries
--app=APPLICATION - alter only selected application
[you need clone this repo](https://bitbucket.org/certator/django_snippets)
This class makes easier the job of rendering lists of model instances in django templates. It's intended to mimic the behavior of the Model Forms in that it contains the code needed to render it as an HTML table and makes it easy to handle all the model lists from a single view (as it's usually done with the generic views for creating and updating model instances).
It also supports pagination and provides hooks for subclassing and customizing the rendered fields, column titles and list order.
Basic example:
`class Account(Model):`
`name = models.CharField(max_length=MAX_LENGTH)`
`responsible = models.CharField(max_length=MAX_LENGTH)`
`email = models.EmailField()`
`class AccountModelList(ModelList):`
`class Meta:`
`model = Account`
`fields = ['name', 'responsible'] #email won't get a column`
The model list would be instantiated with something like:
`model_list = AccountModelList(instances=account_queryset)`
Then a table header can be rendered with model_list.as_table_header(), while the table rows can be rendered calling as_table() on each model_list.items element.
Two template tag filters that can be used to create a table from a sequence.
<table>
{% for row in object_list|groupby_columns:3 %}
<tr>
{% for obj in row %}
<td>{{ obj }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
The example above would create a table where items read from top to bottom, left to right, in 3 columns. "Empty" cells are added to the sequence by the filter so that your rows balance.
*WARNING* This is *extremely* slow.
This snippet allows you to easily prevent *most* race conditions (if used properly).
Feel free to extend on top of this as you like, I'd appreciate any comments to [email protected]
Ever wished you could have pretty SQL-like output for a python object (e.g., a list of dicts) while you're debugging your code? This function will do just that. Simply pass it an object that is an iterable of dictionaries and it returns it in an easy-to-read table, similar to the output of commandline SQL.
Example:
from tablelize import tableize
from django.contrib.auth.models import User
print(tableize(User.objects.values('email', 'first_name', 'last_name')))
+------------+-----------+-------------------+
| first_name | last_name | email |
+------------+-----------+-------------------+
| Test | User | [email protected] |
| Another | User | [email protected] |
+------------+-----------+-------------------+
This table tag helps with render tables, which can be fairly complex.
I updated the previous table tag (296).
I added support for oddrow,evenrow,lastcellinrow,oddcol,evencol. And made a few minor adjustments to syntax formatting, and some non needed if conditionals
These are all of the supported variables available in the context
{{table.counter0}}
{{table.counter}}
{{table.rowcounter0}}
{{table.rowcounter}}
{{table.startrow}}
{{table.endrow}}
{{table.oddrow}}
{{table.evenrow}}
{{table.firstrow}}
{{table.lastrow}}
{{table.firstcell}}
{{table.lastcell}}
{{table.lastcellinrow}}
{{table.evencol}}
{{table.oddcol}}
{{table.parenttable}}
Sometimes you need to prevent concurrent access to update/calculate some properties right. Here is (MySQL) specific example to lock one table with new object manager functions.
Creates a filter for displaying calendars.
Passed value is a dict of dicts of arrays: that is, indexed by year, then by month. The list of month days is used to generate links in the format specified by the argument. If a particular day is *not* in the list, then no link will be generated and it will just display a number for that day.
**EXAMPLE**
`{{ calendar|calendar_table:"/schedules/calendar/[Y]-[m]-[d]/" }}`
Sometimes we want to render items as cells in table with fixed row-count and computable col-count shape and vice versa. It's not difficult to do it with compute odd and even row, col index, but more common way is to use some reshape function. Theare are simple TAG for it: "table", with use reshape function and FILTER "flatindex" wich can use to get flat index in nested loops (may use with table like in this example).
Example of usage:
`{# List filials must exists in context! #}
{% load table %}
<table border="0" width="100%">
{% table filials "3x?" %}
{% for row in table_obj %}
<tr>
{% for record in row %}
{% if record %}
<td class="mf_table_cell" onclick="selcell('filial_{{forloop|flatindex}}')">
<img src="/art/filial.gif" style="margin-bottom: 4px;"/><br/>
<span id="filial_{{forloop|flatindex}}" class="mf_table_unselcell">{{ record }}</span>
</td>
{% else %}
<td class="mf_table_cell">
</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% endtable %}
</table>`
/best regards
Yosifov Pavel
A widget for selecting from a list of `Model` instances using `MultipleChoiceField` which renders a table row for each choice, consisting of a column for a checkbox followed by a column for each item specified in `item_attrs`, which must specify attributes of the objects passed as choices.
This is rough code that will allow you to create a table using a sequence. It is based on the for loop tag in django template. It works basically the same except that certain variables are set based on what cell is being rendered. The tag itself does not output any html at all. This allows for simple code and very flexible creation of nice tables/grids. Enjoy