web/lib/django/views/generic/date_based.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
    25     model = queryset.model
    25     model = queryset.model
    26     if not allow_future:
    26     if not allow_future:
    27         queryset = queryset.filter(**{'%s__lte' % date_field: datetime.datetime.now()})
    27         queryset = queryset.filter(**{'%s__lte' % date_field: datetime.datetime.now()})
    28     date_list = queryset.dates(date_field, 'year')[::-1]
    28     date_list = queryset.dates(date_field, 'year')[::-1]
    29     if not date_list and not allow_empty:
    29     if not date_list and not allow_empty:
    30         raise Http404, "No %s available" % model._meta.verbose_name
    30         raise Http404("No %s available" % model._meta.verbose_name)
    31 
    31 
    32     if date_list and num_latest:
    32     if date_list and num_latest:
    33         latest = queryset.order_by('-'+date_field)[:num_latest]
    33         latest = queryset.order_by('-'+date_field)[:num_latest]
    34     else:
    34     else:
    35         latest = None
    35         latest = None
   103     """
   103     """
   104     Generic monthly archive view.
   104     Generic monthly archive view.
   105 
   105 
   106     Templates: ``<app_label>/<model_name>_archive_month.html``
   106     Templates: ``<app_label>/<model_name>_archive_month.html``
   107     Context:
   107     Context:
       
   108         date_list:
       
   109             List of days in this month with objects
   108         month:
   110         month:
   109             (date) this month
   111             (date) this month
   110         next_month:
   112         next_month:
   111             (date) the first day of the next month, or None if the next month is in the future
   113             (date) the first day of the next month, or None if the next month is in the future
   112         previous_month:
   114         previous_month:
   137 
   139 
   138     # Only bother to check current date if the month isn't in the past and future objects are requested.
   140     # Only bother to check current date if the month isn't in the past and future objects are requested.
   139     if last_day >= now.date() and not allow_future:
   141     if last_day >= now.date() and not allow_future:
   140         lookup_kwargs['%s__lte' % date_field] = now
   142         lookup_kwargs['%s__lte' % date_field] = now
   141     object_list = queryset.filter(**lookup_kwargs)
   143     object_list = queryset.filter(**lookup_kwargs)
       
   144     date_list = object_list.dates(date_field, 'day')
   142     if not object_list and not allow_empty:
   145     if not object_list and not allow_empty:
   143         raise Http404
   146         raise Http404
   144 
   147 
   145     # Calculate the next month, if applicable.
   148     # Calculate the next month, if applicable.
   146     if allow_future:
   149     if allow_future:
   158 
   161 
   159     if not template_name:
   162     if not template_name:
   160         template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
   163         template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
   161     t = template_loader.get_template(template_name)
   164     t = template_loader.get_template(template_name)
   162     c = RequestContext(request, {
   165     c = RequestContext(request, {
       
   166         'date_list': date_list,
   163         '%s_list' % template_object_name: object_list,
   167         '%s_list' % template_object_name: object_list,
   164         'month': date,
   168         'month': date,
   165         'next_month': next_month,
   169         'next_month': next_month,
   166         'previous_month': previous_month,
   170         'previous_month': previous_month,
   167     }, context_processors)
   171     }, context_processors)
   337     if object_id:
   341     if object_id:
   338         lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id
   342         lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id
   339     elif slug and slug_field:
   343     elif slug and slug_field:
   340         lookup_kwargs['%s__exact' % slug_field] = slug
   344         lookup_kwargs['%s__exact' % slug_field] = slug
   341     else:
   345     else:
   342         raise AttributeError, "Generic detail view must be called with either an object_id or a slug/slugfield"
   346         raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield")
   343     try:
   347     try:
   344         obj = queryset.get(**lookup_kwargs)
   348         obj = queryset.get(**lookup_kwargs)
   345     except ObjectDoesNotExist:
   349     except ObjectDoesNotExist:
   346         raise Http404, "No %s found for" % model._meta.verbose_name
   350         raise Http404("No %s found for" % model._meta.verbose_name)
   347     if not template_name:
   351     if not template_name:
   348         template_name = "%s/%s_detail.html" % (model._meta.app_label, model._meta.object_name.lower())
   352         template_name = "%s/%s_detail.html" % (model._meta.app_label, model._meta.object_name.lower())
   349     if template_name_field:
   353     if template_name_field:
   350         template_name_list = [getattr(obj, template_name_field), template_name]
   354         template_name_list = [getattr(obj, template_name_field), template_name]
   351         t = template_loader.select_template(template_name_list)
   355         t = template_loader.select_template(template_name_list)