Forums

django-tables2 templatecolumn

Hi,

I'm using django-table2 to show data from a database query in an HTML table. In my last column I would like to show a button, which should pass some data related to the queried data to a new view. All I could find in the docs was:

class ExampleTable(tables.Table):
    foo = tables.TemplateColumn('{{ record.bar }}')
    # contents of `myapp/bar_column.html` is `{{ value }}`
    bar = tables.TemplateColumn(template_name='myapp/name2_column.html')

But still I have no clue how to solve my problem. Does anybody has an example how to do this?

Thanks

Hi KBrothers,

Do you think you could explain more what you mean by create a button to pass data to a new view?

I just made a quick demo that you could take a look at here.

I made it using python3.4 and django 1.6.6

You can checkout the source code on github here.

Using pythonanywhere, you can also take a look at the actual files and play around on the console etc. (login with username quickdemo, and password demo) But I am probably going to change the password after you are done looking at it though- so say in a week or so.

Thanks a lot for that demo. I think I will try to use a similar approach by adding a Django form with a hidden field and a submit button. Could this work out?

Yup that should work.

I somehow do not succeed. Just to give a more detail explanations.

I'm doing a query on a database and show the data in django-tables2. The table contains an ID column with unique IDs for each row. What I want is a column with buttons. Each button should send on click the ID in the ID column to my view (for example via request.method == 'POST') so that I can continue process it.

I could not find in your demo a hint how your button was sending specific data from specific rows.

The TemplateColumn uses the Django template language to render a column, so you can put html in there to render a form for each button and then use the {{ record.id }} syntax to put the id into a hidden form field.

Thanks a lot Glenn. It works as you said.

Thanks for confirming!

I have to edit values in my django-tables2. Do you know what is the best way to do it? I thought I could define all the columns as input boxes and initialize them with values from my MYSQL table. But it is not displaying the values. Below is my code from tables.py. Any help would be much appreciated.

import django_tables2 as tables
from costtool.models import Ingredients

TEMPLATE = """
<input id="edLevel" maxlength="100" name="count" type="text" value="{{Ingredients.edLevel}}"/>
"""

class IngredientsTable(tables.Table):
    edLevel = tables.TemplateColumn(TEMPLATE)

    class Meta:
        model = Ingredients

If I add a render_edLevel function, then the field displays values correctly but is no longer an input field.

import django_tables2 as tables
from costtool.models import Ingredients

TEMPLATE = """
<input id="edLevel" maxlength="100" name="count" type="text"/>
 """

class IngredientsTable(tables.Table):
    edLevel = tables.TemplateColumn(TEMPLATE)

    def render_edLevel(self, value):         
        return '%s' % value

    class Meta:
        model = Ingredients

I'm just making a guess here, but I would suspect that render_edLevel is supposed to return all the HTML that you want to render.

Ok. Do you know of any way to make the table editable? thanks!

Do you have a URL that shows the problem? Send us a link via "Send feedback" if you want to keep it private.

I undid my code.

Decided to use jQuery sheet instead of Django tables2.

If that also doesn't work, I might come back to Django tables2 :-).

Fair enough :-) Doing it in JavaScript will probably make it a bit prettier than trying to do it on the server side, anyway.