0

I just read the folowing article and i would like more information about the first method using TemplateColumn. I would like to produce two pseudo columns for edit and delete methods of each record.

edit.html

> <a href="{% url some_url_edit record.pk %}" class="tbl_icon
> edit">Edit</a>

delete.html

> <a href="{% url some_url_del record.pk %}" class="tbl_icon
> delete">Delete</a>

2 pseudo columns that does not exist in DB

class MyTable(tables.Table):
column_edit = tables.TemplateColumn(edit.html) column_delete=tables.TemplateColumn(delete.html)

If that is correct according to the article how record.pk is passed on every template to get the required information about its key?

Community
  • 1
  • 1
asimkon
  • 915
  • 1
  • 14
  • 21

2 Answers2

0

If you want to edit or delete an object then you'd need to use a model table:

class MyModelTable(tables.Table):
    name = tables.columns.Column()
    edit = tables.TemplateColumn('<a href='{% url "edit_my_model_instance" record.id %}'>Edit</a>', verbose_name=u'Edit', )    
    delete = tables.TemplateColumn('<a href='{% url "del_my_model_instance" record.id %}'>Delete</a>', verbose_name=u'Delete', )    

    class Meta:
        model = models.MyModel

Notice how we use record.id to pass it the id of each row to the url template tag in order to output the correct edit/delete url.

Serafeim
  • 14,962
  • 14
  • 91
  • 133
  • In case we want to display link images instead of link text with verbose_name how this happens? Should we place somewhere a class and how we define it above in this code? – asimkon Apr 26 '16 at 17:30
  • Using the TemplateColumn you can display whatever you want. For example, if instead of the Edit text you want to display an image you can just use `edit = tables.TemplateColumn('', verbose_name=u'Edit', )` – Serafeim Apr 26 '16 at 20:27
0

i am afraid that your code is a bit faulty as someone must use the following commands to get properly displayed the images inside the columns: edit = tables.TemplateColumn('<a href="{% url "edit_division" record.pk %}"><img src=\'{% load staticfiles %} {% static "images/edit.jpg" %}\' / width="25"></a>',verbose_name=u'Edit',)

 delete = tables.TemplateColumn('<a href="{% url "delete_division" record.pk %}"><img src=\'{% load  staticfiles %} {% static "images/delete.jpg" %}\' / width="25"></a>',verbose_name=u'Delete',)   

I would like to ask if i could use inside tables.py bootstrap css for better rendering of columns and data tables?

asimkon
  • 915
  • 1
  • 14
  • 21
  • Yes you can - I think you should add a `class='table'` to your table's `Meta`. – Serafeim Apr 27 '16 at 10:36
  • I have used successfully in my tables.py the following command attrs = {"class":"paleblue"}, but i want to use two classes similar to attrs = {"class":["paleblue","table-responsive"]} for mobile friendly but it does not work ??? thanks – asimkon Apr 28 '16 at 09:19
  • I think that paleblue is different than the bootstrap classes - I recommend using only the "table table-responsive" classes. – Serafeim Apr 28 '16 at 10:00
  • I tried to use attrs = {"class":"table"} in tables.py and i got nothing on rendering. Should i include a css as header in the template and which file. Besides is there a command where i can use many classes together similar to attrs = {"class":["paleblue","table table-responsive"]}. I think paleblue is only a theme class included default inside djnago-tables2. Thanks – asimkon Apr 28 '16 at 11:44
  • Yes, paleblue is the default theme of djagno-tables2, but I think that if you have included the bootstrap.css you'll get the bootstrap styling by adding the class "table". Also, from what I see, django-tables2 provides a special template for bootstrap, check the example here: https://github.com/bradleyayers/django-tables2/blob/214b83dd329a60c607db06a7f15b17ebd64e1de4/example/app/tables.py – Serafeim Apr 28 '16 at 11:53