我有一个电影和电视节目的数据库。我想像这样过滤这些:/productions/ = index (all), /productions/films/ = only movies, and /productions/series/ = only tv shows
## urls.py
from django.conf.urls import patterns, url
from productions import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^films/$', views.IndexView.as_view(), name='films'),
url(r'^series/$', views.IndexView.as_view(), name='series'),
)
## views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic
from productions.models import Production, Director
class IndexView(generic.ListView):
template_name = 'productions/index.html'
context_object_name = 'productions_list'
def get_queryset(self):
return Production.objects.order_by('-release')
这样的事情的最佳做法是什么?在 views.py 中为每个方法创建一个新方法,或者我可以重用 main 方法,并通过以某种方式解析 URL 段来调用类似 if(productions.is_movie) 的方法?