2

Using djt2 v0.15/dj1.6/pyth2.6.6

djt2 doc example views file for multiple tables:

def people_listing(request) :

    config = RequestConfig(request)
    table1 = PeopleTable(Person.objects.all(), prefix="1-")
    table2 = PeopleTable(Person.objects.all(), prefix="2-")
    config.configure(table1)
    config.configure(table2)
    return render(request, "people_listing.html",
        {"table1": table1, "table2": table2})

This example first of all seems incorrect as to the quoted "table1", "table2" parameters. My tests show the the definition name "people_list" needs to be used in the quotes, at least on a single table. Besides why would anyone want to display the same table twice? Is this a bad example? Here is my app trying to use this structure:

def AvLabVw(request):

    config = RequestConfig(request)
    cmutbl = CmuTable(CmuVersion.objects.all(), prefix="1-")
    simtbl = SimTable(Simulator.objects.all(), prefix="2-")
    config.configure(cmutbl)
    config.configure(simtbl)
    return render(request, "AvRelInfo.html", {"AvLabVw":cmutbl, "AvLabVw":simtbl})

The url file picks up on AvLabVw and the html template uses render_table.

{% render_table AvLabVw %}

What happens with this code is only one table is still displayed, whichever is last on the return render line.

Elsewhere in the doc it says SingleTableView with get_context_data needs to be used, which I haven't figured out yet...

I have an attempt on this style implementation, I think it needs a table object and a list object?

views.py

from django_tables2 import views
from django_tables2 import SingleTableView
from django_tables2 import SingleTableMixin
from django.shortcuts import render
from django_tables2   import RequestConfig

def SimVers_lst(request):

     return render(request, 'AvRelInfo.html', {'SimVers_lst' : Simulator.objects.all()})

def AvLabVw(request):

    config = RequestConfig(request)
    simlst = SimVers_lst(Simulator.objects.all())
    table = CmuTable(CmuVersion.objects.all(), prefix="1-")
    Stv = views.SingleTableView()
    multitbl = Stv.get_context_data()
    config.configure(multitbl)
    return render(request, "AvRelInfo.html", { "AvLabVw" : multitbl })

barfs at {% render_table AvLabVw %} in the html template with the usual catch-all "ValueError at /AvCtlapp/ Expected table or queryset, not 'str'." ... getting some garbage... I guess I can try to see what it gets in a shell if I can set up that test...

Thanks for any help...

Joe

PS: Is a custom render needed, and how would that look?

1 Answer 1

7

Your first code example(and it was copied from django-tables2 documentation) was meant for rendering two tables in one single page. Its not a bad example (I think) because it showed how to render 2 tables from same table class with same query-set with different prefix.

And last code example from your question, you got it wrong about using SingleTableView. Its meant for rendering one table in template and its basically a Class Based View. Try like this:

class AvLabVw(SingleTableView):
    model = Simulator
    template_name = 'AvRelInfo.html'
    table_class = SimulatorTable

and template is like:

{% load render_table from django_tables2 %}
{% render_table table %}

Now if you want to render multiple tables, override the get_context_data() method from this view, like this:

class AvLabVw(SingleTableView):
    model = Simulator
    template_name = 'AvRelInfo.html'
    table_class = SimulatorTable


def get_context_data(self, **kwargs):
    context = super(AvLabVw, self).get_context_data(**kwargs)
    context['table_cmu'] =  CmuTable(CmuVersion.objects.all(), prefix="1-")
    return context

and template like:

{% load render_table from django_tables2 %}
{% render_table table %}
{% render_table table_cmu %}

and urls:

url(r'^something/$', AvLabVw.as_view(), name='avlabvw'),
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for the clean-up, serafeim, couldn't seem to get the syntax to be picked up...
Thanks so much ruddra for the input, I will be dissecting it. So much to know about, will have to look up that "super" operation. So I guess you are prehaps changing the get_context_data method, using Python OO here. Wondering if that is called "extending" the method, (comparing this with Java terminology). And the AvLabVw is an OO child of the generic SingleTableVw Class? hmmm, definitely a need for this multi-table processing in the django world. If/when I get this operating I want 4 tables on 1 page.
Disregard my criticism of the context in the documentation multiple tables example, I may have been able to make the { "AvLabVw" : tablevals } work but I found the definition of context today in The Django template language: For Python programmers online documentation, and understand why you would want those to be the same ;-)
well, happy to help :)
BTW, it would be great if you accept this as answer, if it was helpful to you, and it will be great for other users to see that the question was answered if they face similar issue @JosephCamaioni . And welcome to stackoverflow :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.