How to get information from a Django_tables2 line?

I have declared a table and want to get the value of a row that is checked using checkboxfield. Any help how I can record this event in my views, so that every time I select a row and press the submit button, it returns the values โ€‹โ€‹of the rows. The code is as follows:

class mytables(tables.Table): new_database = tables.CheckBoxColumn() student =tables.Column(accessor='Student') Class = tables.Column(accessor='class') 

And in my templates, the submit button.

+4
source share
1 answer

You need to select the appropriate value for CheckBoxColumn . Typically, if you show a set of queries, you will use pk for each object for CheckBoxColumn . In your case, it will look like this:

 class EnrollmentTable(tables.Table): selection = tables.CheckBoxColumn(accessor='pk') student = tables.Column() class = tables.Column() 

Then you will need to display the table in the form so that the user can submit the form, for example:

 <form action="/someurl/" method="post"> {% load render_tables from django_tables2 %} {% render_table table %} <input type="submit"/> </form> 

Then you need a view connected to /someurl/ . In your case, the view should look at the POST selection variable:

 def someview(request): if request.method == "POST": pks = request.POST.getlist("selection") selected_objects = SomeModel.objects.filter(pk__in=pks) # do something with selected_objects else: # ... 
+14
source

Source: https://habr.com/ru/post/1415572/


All Articles