如何将ListView类用于两个不同的模板

时间:2021-02-16 12:10:20

I have a ListView class in view.py

我在view.py中有一个ListView类

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic

from entertainment.models import Entertainmentblog

class ListView(generic.ListView):
      template_name = 'entertainment/index.html'
      context_object_name = 'latest_article_list'
      slug = None
      id = None

def get_queryset(self):

      return Entertainmentblog.objects.order_by('-posted')[:25]

class DetailView(generic.DetailView):
      model = Entertainmentblog
      template_name = 'entertainment/article.html'      

and I'm using this view to display a list of articles in index.html.But,I would like to show the same list of articles in article.html after the article.I have used the blocks correctly but,it won't show any articles because in ListViewthe template name is index.html.How do I solve this?

我正在使用此视图在index.html中显示文章列表。但是,我想在文章之后显示article.html中相同的文章列表。我已正确使用了块但是,它不会显示任何文章,因为在ListView中模板名称是index.html。我如何解决这个问题?

2 个解决方案

#1


0  

Use a Mixin:

使用Mixin:

class LatestArticleMixin(object):

    def get_context_data(self, **kwargs):
        context = super(LatestArticleMixin, self).get_context_data(**kwargs)
        try:
            context['latest_article_list'] = Entertainmentblog.objects.order_by('-posted')[:25]
        except:
            pass
        return context

Then refactor your DetailView:

然后重构您的DetailView:

class DetailView(LatestArticleMixin, generic.DetailView):
    model = Entertainmentblog
    template_name = 'entertainment/article.html'

In your template if there are articles:

在您的模板中,如果有文章:

{% if latest_article_list %}
    ....

{% endif %}

#2


-1  

In urls.py you can set template_name as attribute to ListView url entry router.

在urls.py中,您可以将template_name设置为ListView url条目路由器的属性。

urls.py

urlpatterns = patterns('',
    (r'^a/$', ListView.as_view(model=Poll, template_name="a.html")),
    (r'^b/$', ListView.as_view(model=Poll, template_name="b.html")),
)

In views.py even you don't need to set template.

在views.py中,即使您不需要设置模板。

views.py

class ListView(generic.ListView):
    model = Poll

#1


0  

Use a Mixin:

使用Mixin:

class LatestArticleMixin(object):

    def get_context_data(self, **kwargs):
        context = super(LatestArticleMixin, self).get_context_data(**kwargs)
        try:
            context['latest_article_list'] = Entertainmentblog.objects.order_by('-posted')[:25]
        except:
            pass
        return context

Then refactor your DetailView:

然后重构您的DetailView:

class DetailView(LatestArticleMixin, generic.DetailView):
    model = Entertainmentblog
    template_name = 'entertainment/article.html'

In your template if there are articles:

在您的模板中,如果有文章:

{% if latest_article_list %}
    ....

{% endif %}

#2


-1  

In urls.py you can set template_name as attribute to ListView url entry router.

在urls.py中,您可以将template_name设置为ListView url条目路由器的属性。

urls.py

urlpatterns = patterns('',
    (r'^a/$', ListView.as_view(model=Poll, template_name="a.html")),
    (r'^b/$', ListView.as_view(model=Poll, template_name="b.html")),
)

In views.py even you don't need to set template.

在views.py中,即使您不需要设置模板。

views.py

class ListView(generic.ListView):
    model = Poll