render_to_response的第一个参数为模板名称,比如templates下的文件books.html,第二个参数为字典。例如:render_to_response('books.html', {'Publisher_line':Publisher_select_all)
在实验中,如果想在第二个参数中放置两个字典,比如
render_to_response('books.html', {'Publisher_line':Publisher_select_all,'Book_line':Book_select_all})
此时,第二个参数将会被忽略,在生成的网页文件中,只会出现第一个参数,要解决这个问题,可以使用一个字典的集合。例如
Publisher_select_all = Publisher.objects.all()
Book_select_all = Book.objects.all()
sql_dic = {'Publisher_line':Publisher_select_all,'Book_line':Book_select_all}
return render_to_response('books.html',sql_dic)
或直接写多个字典的集合
return render_to_response('books.html', {'Publisher_line':Publisher_select_all,'Book_line':Book_select_all})
到此,问题解决,两个变量均能在页面中显示。
在Template模版页面获取sql_dic变量的值可以通过使用{
{ Publisher_line }}和{ { Book_line }}来分别获取这两个字典的值。# vi books.html
{% extends "base.html" %} {% block title %}应用 books 示例{% endblock %} {% block content %} 示例 Publisher select_all: <ul> {% for item in Publisher_line %} <li> { {item.name}}|{ {item.city}}|{ {item.country}} </li> {% endfor %} </ul> 示例 Book select_all: <ul> {% for item in Book_line %} <li> { {item.title}}|{ {item.publication_date}}|{ {item.publisher_id}} </li> {% endfor %} </ul> {% endblock %}