def homepage(request): try: issue, cache_key = check_smart_cache(request, 'homepage_view') if not issue: issue = Issue.objects.filter(published=True).latest('published_at') cache.set(cache_key, issue) cache.set('%s.stale' % cache_key, STALE_CREATED) update_cache_dependency(request, issue, cache_key) return render(request, ['homepage/%s' % issue.display_type.template_name, 'homepage/default'], {'issue': issue}) except Issue.DoesNotExist: return render(request, ['homepage/default'])
def event_archive(request, year=None, month=None, day=None): kwargs = {} if year: kwargs['date__year'] = int(year) if month: kwargs['date__month'] = int(month) if day: kwargs['date__day'] = int(day) events = Event.objects.filter(**kwargs).order_by('-date') #Paginate paginator = Paginator(events, 35, 5) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 # If page request (9999) is out of range, deliver last page of results. try: events_paginated = paginator.page(page) except (EmptyPage, InvalidPage): events_paginated = paginator.page(paginator.num_pages) return render(request, ['events/archive'], {'events': events_paginated})
def event_submit(request): if request.method == 'POST': form = EventForm(request.POST) if form.is_valid(): form.save() # do something. else: form = EventForm() return render(request, ["events/submit"], {"form": form})
def media_archive(request, type, template=None): for model in models.get_models(): if issubclass(model, MediaItem) and not model is MediaItem and model._meta.verbose_name_plural == type: template_search = [ template, 'media/%s/archive' % model._meta.verbose_name, 'media/archive', ] return render(request, template_search, {'model': model, 'model_name': model._meta.verbose_name_plural }) raise Http404
def media_detailed(request, slug=None, year=None, month=None, day=None, template=None): kwargs = { 'slug':slug } if year and month and day: kwargs['published_at__year'] = int(year) kwargs['published_at__month'] = int(month) kwargs['published_at__day'] = int(day) media_item = MediaItem.objects.get(**kwargs).as_leaf_class() return render(request, [template, 'media/%s/detailed' % media_item.content_type.name.lower().replace(' ', '_'), 'media/detailed'], {'media_item':media_item})
def section_detailed(request, path): if path.startswith('/'): path = path[1:] if path.endswith('/'): path = path[:-1] section = get_object_or_404(Section, full_path=path) template_search = ["sections/%s/detailed" % path, ] while path.count('/') > 0: path = path[:path.rfind('/')] template_search.append("sections/%s/subsection_detailed" % path) template_search.append("sections/%s/detailed" % path) template_search.append("sections/detailed") return render(request, template_search, {'section': section})
def article_detailed(request, section=None, slug=None, year=None, month=None, day=None, template=None): article, cache_key = check_smart_cache(request, 'article_view', slug, year, month, day) if not article: kwargs = {'section__full_path': section, 'slug': slug} if year and month and day: kwargs['published_at__year'] = int(year) kwargs['published_at__month'] = int(month) kwargs['published_at__day'] = int(day) if not request.user.is_superuser: kwargs['status__published'] = True article = Article.objects.get(**kwargs) cache.set(cache_key, article) cache.set('%s.stale' % cache_key, STALE_CREATED) update_cache_dependency(request, article, cache_key) return render(request, [template, 'articles/%s/%s' % (article.section.path, article.display_type.template_name), 'articles/%s' % article.display_type.template_name, 'articles/%s' % settings.DISPLAY_TYPE_TEMPLATE_FALLBACK], {'article': article})
def staffer_detailed(request, slug): staffer = get_object_or_404(Staffer, slug=slug, public_profile=True) return render(request, ['staff/custom/%s' % staffer.slug, 'staff/detailed'], {'staffer': staffer})
def event_detailed(request, slug, year, month, day): kwargs = {'slug': slug, 'date': datetime.datetime(int(year), int(month), int(day))} event = Event.objects.get(**kwargs) return render(request, ['events/detailed'], {'event': event})
def render_page(request, url): page = Page.objects.get(url=url) #[:-5] is to strip the HTML return render(request, ['pages'+page.template[:-5],])
def most_popular(request, type='articles', days=7): return render(request, ['most_popular/most_popular'], {'type': type, 'days': int(days)})
def tag_detailed(request, tag_name): tag = Tag.objects.get(name=tag_name) return render(request, ['tags/custom/%s' % tag_name, 'tags/detailed'], {'tag': tag})
def issue_archive(request, year, month, day): issue = Issue.objects.get(published_at__year=int(year), published_at__month=int(month), published_at__day=int(day)) return render(request, ['issues/archive'], {'issue': issue})