主题:generic views 新掌握的一个方法;)--extra_context
时间: 2020-08-27来源:ITEYE
前景提要
相关推荐: 初识django, 一起进入python的世界吧 django心得:url可以做的更多 django的debug_toolbar 在windows下详细安装步骤 Generic Views odoo13源码-004: models_1 第九章: Generic views Python著名的lib和开发框架(均为转载) 翻译www.djangobook.com之第九章: Generic views
推荐群组: Tapestry
更多相关推荐
Python 今天要写一个程序的小模块---同一个网页里面,中间的内容区要显示所有的student,而在右侧边栏需要显示所有的class(班级;))起初已经写完了显示所有student的代码了,用的就是generic views----太爽了,仅用了很少很少的代码。 newsList = {     'queryset': Student.objects.all(), } urlpatterns = patterns('',     (r'^$', 'django.views.generic.list_detail.object_list',dict(paginate_by=20, **newsList)), ) note :        paginate_by=20指每20个记录分页        默认的模版名字为     (你的app名称)_list.html,如你的app为stu,那么默认模版就为stu_list.html 接下来需求变更,要求我在同一页面显示class列表... 呵呵,因为我比较懒,所以一来我不想修改现有代码,二来不愿意重新写views中关于查询数据库然后传递到模版等代码。我想generic views一定已经解决了同时传多个字典的情况了。呵呵,果然:你只需添加extra_context就可以了--还是非常少的代码 newsList = {     'queryset': Student.objects.all(),     'extra_context': {'classes': Class.objects.all()}, } urlpatterns = patterns('',     (r'^$', 'django.views.generic.list_detail.object_list',dict(paginate_by=20, **newsList)), ) 在模版里,你只需取classes的值就可以了,如: {% for item in classes%}             {{ item.classes_name }} {% endfor %}

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行