Jos*_*oni 2 python view multiple-tables django-tables2
使用 djt2 v0.15/dj1.6/pyth2.6.6
多个表的 djt2 doc 视图文件示例:
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})
Run Code Online (Sandbox Code Playgroud)
首先,该示例对于引用的“table1”、“table2”参数似乎不正确。我的测试显示定义名称“people_list”需要在引号中使用,至少在单个表上。此外,为什么有人想将同一张表显示两次?这是一个坏例子吗?这是我的应用程序尝试使用这种结构:
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})
Run Code Online (Sandbox Code Playgroud)
url 文件在 AvLabVw 上选取,html 模板使用 render_table。
{% render_table AvLabVw %}
Run Code Online (Sandbox Code Playgroud)
此代码所发生的情况是仅仍然显示一个表,以返回渲染行上的最后一个表为准。
在文档的其他地方,它说需要使用具有 get_context_data 的 SingleTableView ,我还没有弄清楚......
我尝试过这种风格的实现,我认为它需要一个表对象和一个列表对象?
从 django_tables2 导入视图
从 django_tables2 导入 SingleTableView
从 django_tables2 导入 SingleTableMixin
从 django.shortcuts 导入渲染
从 django_tables2 导入 RequestConfig
def SimVers_lst(请求):
返回渲染(请求,'AvRelInfo.html',{'SimVers_lst':Simulator.objects.all()})
def AvLabVw(请求):
配置=RequestConfig(请求)
simlst = SimVers_lst(Simulator.objects.all())
表 = CmuTable(CmuVersion.objects.all(), prefix="1-")
Stv =views.SingleTableView()
multitbl = Stv.get_context_data()
config.configure(多表)
返回渲染(请求,“AvRelInfo.html”,{“AvLabVw”:multitbl })
barfs{% render_table AvLabVw %}在 html 模板中使用通常的包罗万象
"ValueError at /AvCtlapp/ Expected table or queryset, not 'str'."
...得到一些垃圾...我想我可以尝试看看它在 shell 中得到什么,如果我可以设置该测试...
谢谢你的帮助...
乔
PS:是否需要自定义渲染,效果如何?
您的第一个代码示例(它是从 django-tables2 文档复制的)旨在在一个页面中呈现两个表。它是一个不错的例子(我认为),因为它展示了如何使用具有不同前缀的相同查询集从同一表类渲染 2 个表。
你的问题的最后一个代码示例,你在使用 SingleTableView 时弄错了。它的目的是在模板中渲染一张表,它基本上是一个基于类的视图。尝试这样:
class AvLabVw(SingleTableView):
model = Simulator
template_name = 'AvRelInfo.html'
table_class = SimulatorTable
Run Code Online (Sandbox Code Playgroud)
模板如下:
{% load render_table from django_tables2 %}
{% render_table table %}
Run Code Online (Sandbox Code Playgroud)
现在,如果您想呈现多个表,请重写get_context_data()此视图中的方法,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
和模板如:
{% load render_table from django_tables2 %}
{% render_table table %}
{% render_table table_cmu %}
Run Code Online (Sandbox Code Playgroud)
和网址:
url(r'^something/$', AvLabVw.as_view(), name='avlabvw'),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9453 次 |
| 最近记录: |