Nah*_*ing 35 django templates model views
我正在建立一个地址簿,其中包括条目之间的关系等.我有个人,公司,场地和角色的单独模型.在我的索引页面上,我想列出每个模型的所有实例,然后过滤它们.这样一个人就可以轻松搜索并找到一个条目.我已经能够使用通用视图列出单个模型,并使用get_extra_context来显示另一个模型:
#views.py
class IndividualListView(ListView):
context_object_name = "individual_list"
queryset = Individual.objects.all()
template_name='contacts/individuals/individual_list.html'
class IndividualDetailView(DetailView):
context_object_name = 'individual_detail'
queryset = Individual.objects.all()
template_name='contacts/individuals/individual_details.html'
def get_context_data(self, **kwargs):
context = super(IndividualDetailView, self).get_context_data(**kwargs)
context['role'] = Role.objects.all()
return context
Run Code Online (Sandbox Code Playgroud)
我还可以使用自定义视图列出单个模型:
#views.py
def object_list(request, model):
obj_list = model.objects.all()
template_name = 'contacts/index.html'
return render_to_response(template_name, {'object_list': obj_list})
Run Code Online (Sandbox Code Playgroud)
以下是这两个测试的urls.py:
(r'^$', views.object_list, {'model' : models.Individual}),
(r'^individuals/$',
IndividualListView.as_view(),
),
(r'^individuals/(?P<pk>\d+)/$',
IndividualDetailView.as_view(),
),
Run Code Online (Sandbox Code Playgroud)
所以我的问题是"如何修改它以将多个模型传递给模板?" 它甚至可能吗?StackOverflow上的所有类似问题只询问两个模型(可以使用get_extra_context解决).
Nah*_*ing 54
我最后修改了@thikonom的答案,使用基于类的视图:
class IndexView(ListView):
context_object_name = 'home_list'
template_name = 'contacts/index.html'
queryset = Individual.objects.all()
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['roles'] = Role.objects.all()
context['venue_list'] = Venue.objects.all()
context['festival_list'] = Festival.objects.all()
# And so on for more models
return context
Run Code Online (Sandbox Code Playgroud)
在我的urls.py中
url(r'^$',
IndexView.as_view(),
name="home_list"
),
Run Code Online (Sandbox Code Playgroud)
thi*_*nom 19
我建议你删除你的object_list观点,
为这个特定的视图定义一个字典,
all_models_dict = {
"template_name": "contacts/index.html",
"queryset": Individual.objects.all(),
"extra_context" : {"role_list" : Role.objects.all(),
"venue_list": Venue.objects.all(),
#and so on for all the desired models...
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的网址中:
#add this import to the top
from django.views.generic import list_detail
(r'^$', list_detail.object_list, all_models_dict),
Run Code Online (Sandbox Code Playgroud)
如果你想在Django 1.5上构建它,你将能够使用稳定版本的CBV.请在下面找到代码.
你可以在这里找到很棒的文档https://docs.djangoproject.com/en/dev/topics/class-based-views/mixins/
class ProductsCategoryList(ListView):
context_object_name = 'products_list'
template_name = 'gallery/index_newborn.html'
def get_queryset(self):
self.category = get_object_or_404(Category, name=self.args[0])
return Products.objects.filter(category=self.category)
def get_context_data(self, **kwargs):
kwargs['category'] = Category.objects.all()
# And so on for more models
return super(ProductsCategoryList, self).get_context_data(**kwargs)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34433 次 |
| 最近记录: |