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?

Serafeim
  • 14,962
  • 14
  • 91
  • 133

1 Answers1

4

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'),
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • Thanks for the clean-up, serafeim, couldn't seem to get the syntax to be picked up... – Joseph Camaioni Dec 31 '14 at 16:37
  • 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. – Joseph Camaioni Dec 31 '14 at 16:52
  • 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 ;-) – Joseph Camaioni Dec 31 '14 at 16:56
  • well, happy to help :) – ruddra Dec 31 '14 at 17:22
  • 1
    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 :) – ruddra Jan 01 '15 at 06:25
  • where have u faced problem? – ruddra Jan 06 '15 at 16:58
  • going for 2 tables> `class AvLabVw(SingleTableView): model = Simulator template_name = 'AvCmuSimInfo.html' table_class = SimTable def get_context_data(self, **kwargs): context = super(AvLabVw, self).get_context_data(**kwargs) context['cmutblvals'] = CmuTable(CmuVers.objects.all(), prefix="2-") return context ` In the html template> ` {% render_table table %} {% render_table cmutblvals %}` url> `url(r'^AvCmuSimInfo$', views.AvLabVw.as_view(), name='Avionics Lab MATT Support for CMU/Simulator'),` 2nd render error "Expected table or queryset,not'str'" – Joseph Camaioni Jan 06 '15 at 17:06
  • first render is A-OK when second render is commented out... code quotes not working like I wish... ValueError apparently sees cmutblvals as a string, it must not be getting its assignment. – Joseph Camaioni Jan 06 '15 at 17:16
  • make sure u are importing the CmuTable class properly in the views.py. ps: I have tested the code, and it worked for me(ofcourse with different model/table definitions) – ruddra Jan 06 '15 at 17:32
  • Using global import *. I assume u are using **views**.AvLabVw.as_view() now 'cuz that give me a NameError for AvLabVw without the **views**... I had actually mixed up that I wanted the CmuTable first and SimTable second, noting that the "table" value was still passing the CmuTable deceptively to the first render_table command(second render_table commented out). Maybe printing the Parent class object value then? The evaluation order giving unexpected results? Somebody throw me a towel! Are you using dj1.7/py3+? I'm at dj1.6/py2.6. – Joseph Camaioni Jan 06 '15 at 18:59
  • PS: I have each of these tables working independently defined in the Parent class I guess, and it must be taking the last table variable assignment done by the parent methods? I am setting unique variables otherwise... – Joseph Camaioni Jan 06 '15 at 19:35
  • Probably should throw out those other definitions... Using a single table display method for now and will look at again in the future. – Joseph Camaioni Jan 06 '15 at 20:00
  • print out the table contents you are getting for second table class, check if its a string or anything else. Or u can directly send the queryset in like `context['cmutablvals'] = CmuVers.objects.all()` and check if it works. – ruddra Jan 07 '15 at 08:20