Py学习  »  Django

render_to_response的第二个参数如何使用包含多个查询表结果的数组

江湖人小猫 • 11 年前 • 8042 次点击  

各位好,我最近新用Django写个页面,想要完成如下效果: 有三张表:

表1.table_name:

id | tablename

0 | student

1 | teacher

表2.student(内含学生信息)

表3.teacher(内含老师信息)

view.py原来如下:

def show(request):

  student=student.objects.filter(class="1")

  teacher=teacher.objects.filter(class="1")

  return render_to_response('show.html',{'student':student,'teacher':teacher})

现在想要做到想查的表添加到table_name表里即可,所以想写一个for循环,每次读取table_name的一行,再查询这行记录显示的表名。但render_to_response的第二个参数字典一定是写死的,不能根据for读取的结果随时改变,请问该怎么修改?

我的初步想法是把查询到的结果数组(诸如student、teacher)再塞进一个数组table_all,再写成:

return render_to_response('show.html',{'table_all':table_all})

但怎么做都无法成功。求各位大神指点。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/538
 
8042 次点击  
文章 [ 3 ]  |  最新文章 10 年前