我有一個模板,我需要從多個模型呈現信息。我的models.py是這個樣子:Django模板與多個模型
# models.py
from django.db import models
class foo(models.Model):
''' Foo content '''
class bar(models.Model):
''' Bar content '''
我也有一個文件views.py,從中我根據this Django documentation和the answer given here寫道,看起來是這樣的:
# views.py
from django.views.generic import ListView
from app.models import *
class MyView(ListView):
context_object_name = 'name'
template_name = 'page/path.html'
queryset = foo.objects.all()
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['bar'] = bar.objects.all()
return context
和我在urls.py urlpatterns的有以下對象:
url(r'^path$',views.MyView.as_view(), name = 'name'),
我的問題是,在模板頁/ path.html我怎麼可以參考的對象,並從F中的對象屬性oo和bar將它們顯示在我的頁面中?