我正试图在这个Views.py中找到Class MovieList引用模板的地方。
import datetime, calendar
from django.contrib.contenttypes.models import ContentType
from django.views.generic import ListView, DetailView
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from movies.models import Movie, Show
from common.views import StatsDetailView
from common.models import Category, HotItem
from directory.models import Venue
import tasks
def _get_movie_dates():
###
# Return list of dates for all future dates with movies showing
###
return Movie.objects.filter(shows__starts__gte=datetime.date.today()).values_list('shows__starts', flat=True).order_by('shows__starts').distinct()
def _get_now_showing():
###
# Return list of movies for now showing
###
return Movie.objects.filter(shows__starts__gte=datetime.date.today()).order_by('name').distinct()
class MovieList(ListView):
model = Movie
paginate_by = 20
context_object_name = 'movies'
category = None
venue = None
date = None
slug_level = ""
def get_queryset(self):
qs = Movie.objects.filter(visible=True,).order_by('-hot', '-showing', 'name')
if self.kwargs.get('category', None):
slugs = self.kwargs['category'].strip('/').split('/')
self.category = get_object_or_404(Category, slug=slugs[-1])
category_ids = [c.id for c in self.category.get_child_categories()]
category_ids.append(self.category.id)
qs = qs.filter(categories__in=category_ids)
if self.kwargs.get('venue', None):
self.venue = get_object_or_404(Venue, slug=self.kwargs['venue'])
venue_ids = [v.id for v in Venue.objects.filter(parent=self.venue)]
venue_ids.append(self.venue.id)
qs = qs.filter(shows__venue__in=venue_ids)
if self.kwargs.get('shortcut', None):
today = datetime.date.today()
shortcut = self.kwargs['shortcut']
if shortcut == 'now-showing':
qs = qs.filter(shows__starts__gte=today,)
elif shortcut == 'today':
qs = qs.filter(shows__starts__exact=today)
elif shortcut == 'tomorrow':
qs = qs.filter(shows__starts__exact=today + datetime.timedelta(days=1))
elif shortcut == 'this-weekend': #Friday - Sunday
days = 4 - today.weekday()
starts = today + datetime.timedelta(days=days)
ends = starts + datetime.timedelta(days=2)
qs = qs.filter(shows__starts__range=(starts, ends))
elif shortcut == 'tickets':
qs = qs.filter(
shows__starts__gte=today,
shows__venue__name__icontains='imax',
visible=True
)
if self.kwargs.get('date', None):
d = datetime.datetime.strptime(self.kwargs['date'], "%Y-%m-%d").date()
self.date = d
qs = qs.filter(shows__starts__exact=d)
return qs.distinct()
def get_context_data(self, **kwargs):
context = super(MovieList, self).get_context_data(**kwargs)
today = datetime.date.today()
#context['categories'] = Category.objects.filter(parent__slug='movies', visible=True,)
context['venues'] = Venue.objects.filter(show__starts__gte=today, parent=None, visible=True,).distinct()
context['category'] = self.category
context['venue'] = self.venue
context['date'] = self.date
context['dates'] = _get_movie_dates()
context['now_showing'] = _get_now_showing()
context['nos'] = len(self.slug_level)
return context
class MovieDetail(StatsDetailView):
model = Movie
context_object_name = 'movie'
def get_context_data(self, **kwargs):
context = super(MovieDetail, self).get_context_data(**kwargs)
today = datetime.date.today()
#context['categories'] = Category.objects.filter(parent__slug='movies', visible=True,)
context['venues'] = Venue.objects.filter(show__starts__gte=today, parent=None, visible=True,).distinct()
context['dates'] = _get_movie_dates()
context['now_showing'] = _get_now_showing()
return context
def buy_movie(req, slug):
movie = get_object_or_404(Movie, slug=slug)
url = 'http://rdtickets.buymore.co.ke/api/buy?movie=%d' % movie.pk
return render_to_response(
'movies/movie_buy.html',
locals(),
RequestContext(req, {})
)
def refresh_buy_more(req):
movies = {}
venues = {}
shows = Show.objects.filter(starts__gte=datetime.date.today())
for show in shows:
movies[show.movie.pk] = show.movie
venues[show.venue.pk] = show.venue
for movie in movies:
tasks.post_movie_to_buymore(movies[movie])
for venue in venues:
tasks.post_venue_to_buymore(venues[venue])
for show in shows:
tasks.post_show_to_buymore(show)
return HttpResponse('synced')
没有提及模板,我可以找到任何帮助来确定模板的位置。
答案 0 :(得分:3)
Django docs解释了视图如何确定要使用的模板:
这就是我们需要编写的所有Python代码。但是,我们仍然需要编写模板。我们可以通过向视图添加
template_name
属性来明确告诉视图使用哪个模板,但是如果没有显式模板,Django将从对象的名称推断出一个。在这种情况下,推断的模板将是"books/publisher_list.html"
- “books”部分来自定义模型的应用程序的名称,而“publisher”位只是模型名称的小写版本。
因此,如果您未设置template_name
,则默认模板为movies/movie_list.html
。