django较多数据传递如何优雅的呈现,django较多传递,最近在使用django
django较多数据传递如何优雅的呈现,django较多传递,最近在使用django
最近在使用django 开发一个网站,views 在渲染模板的时候,总是有很多数据要传递给模板,使得最后的return 语句相当难看,没有什么易读性。
return render_to_response(
'test.html',
{
'test1': test1,
'test2': test2,
'test3': test3,
'test4': test4,
'test5': test5,
'test6': test6,
'test7': test7,
'test8': test8,
'test9': test9,
'test10': test10,
'test11': test11,
'test12': test12,
'test13': test13,
'test14': test14,
'test15': test15,
'test16': test16,
'test17': test17,
'test18': test18,
}
) 请问有什么优雅的方式可以传递较多的数据给模板。
懒一点的直接用
return render(request, 'test.html', locals())
曾经profile过的人表示,什么模板渲染效率(说渲染引擎会deepcopy这个kwargs的人是根本没看过源代码!)、时间的,只要模板不是过多include,造成template nodes特别多,就基本上不够数据库处理的零头。
把这么多数据放在一个 dict 里不就完了么。
return render_to_response('test.html', dict_a)
class testData():
t1 = test1
t2 = test2
t3 = test3
.....
然后传一个testData的实例过去就行了。
这样写不就完了么。
return render_to_response('test.html',{'test1': test1,'test2': test2,'test3': test3,'test4': test4,'test5': test5,'test6': test6,'test7': test7,'test8': test8,'test9': test9,'test10': test10,'test11': test11,'test12': test12,'test13': test13,'test14': test14,'test15': test15,'test16': test16,'test17': test17,'test18': test18,})
编橙之家文章,
相关内容
- 不用跳转方法如何实现手机站与pc站之间的无缝切换,
- python3中文输出报UnicodeEncodeError错误,如何解决,,代码
- Python字典操作的问题,要删除字典内部重复值item操作方
- python如何缩短cmd中过长的命令,python缩短cmd过长,比如我
- python if not不同应用有什么区别,pythonifnot区别,python 中
- 新手问题Python字符转义/使用,新手python字符转义,比如
- Python多维元组与列表转换的问题,python多维列表转换
- Python对JS型数据抓取有什么特别好的方法吗,pythonjs型抓
- 使用Python搭建Web服务器来做博客论坛可行吗,pythonweb
- python读取excel数据报xlrd.biffh.XLRDError错误原因,,比如说
评论关闭