我是第一次开发Django应用程序。如何将多个模型传递给模板
views.py
from django.views.generic import ListView, DetailView
from django.utils import timezone
from LastAlly.models import Article, Episode
class IndexView(ListView):
queryset = Article.objects.all().order_by("-date")[:3]
template_name = 'index.html'
urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import ListView, DetailView
from LastAlly.views import IndexView
urlpatterns = [
url(r'^$', IndexView.as_view(), ),
]
答案 0 :(得分:2)
您可以修改视图的方法,在这种情况下,您可以编辑.get_context_data()
方法:
class IndexView(ListView):
queryset = Article.objects.all().order_by("-date")[:3]
template_name = 'index.html'
def get_context_data(self, *args, **kwargs):
context = super(IndexView, self).get_context_data(*args, **kwargs)
context['episode_objects'] = Episode.objects.....
return context
然后在你的模板中将有一个{{ episode_objects }}
变量和Episode模型对象。