【发布时间】:2017-05-31 16:08:42
【问题描述】:
我有一个模板,我需要从中呈现来自多个模型的信息。我的 models.py 看起来像这样:
# 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'),
我的问题是,在模板 page/path.html 上,如何引用 foo 和 bar 中的对象和对象属性以在我的页面中显示它们?
【问题讨论】:
标签: python django django-templates jinja2