基于Django类的视图查找模板

2024-05-19 10:08:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从中找到视图.py其中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')

没有参考模板从什么我可以找到任何帮助,找出模板在哪里将不胜感激。在


Tags: importselfgettodaydatetimedatecontextmovies
1条回答
网友
1楼 · 发布于 2024-05-19 10:08:04

Django docs解释视图如何确定要使用哪个模板:

That’s all the Python code we need to write. We still need to write a template, however. We could explicitly tell the view which template to use by adding a template_name attribute to the view, but in the absence of an explicit template Django will infer one from the object’s name. In this case, the inferred template will be "books/publisher_list.html" – the “books” part comes from the name of the app that defines the model, while the “publisher” bit is just the lowercased version of the model’s name.

所以在您的例子中,如果不设置template_name,默认模板将是movies/movie_list.html。在

相关问题 更多 >

    热门问题