Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

django - How to get information from Django_tables2 row?

I have declared a table and want to fetch the row's value which is checked using checkboxfield. Any help, how can i write this event in my views so that everytime I select a row and hit submit button, it returns the row's values.Code goes like this:

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

And in my templates a submit button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to choose a suitable value for the CheckBoxColumn. Generally if you're displaying a queryset, you'll use the pk of each object for the CheckBoxColumn. In your case this would look like:

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

Then you'll need to render the table within a form, so that the user can submit the form, e.g.:

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

Then you'll need a view hooked up to /someurl/. In your case the view will need to look at the POST variable selection:

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:
        # ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...