Ejemplo n.º 1
0
    def get_template_names(self):
        """
        Return a list of template names to be used for the request. Must return
        a list. May not be called if render_to_response is overridden.
        """
        try:
            names = super(MultipleObjectTemplateResponseMixin, self).get_template_names()
        except ImproperlyConfigured:
            # If template_name isn't specified, it's not a problem --
            # we just start with an empty list.
            names = []

        # If the list is a query, we'll invent a template name based on the
        # app and model name. This name gets put at the end of the template
        # name list so that user-supplied names override the automatically-
        # generated ones.
        if isinstance(self.object_list, Query):
            model = utils.model_from_query(self.object_list)
            package = utils.get_template_package_name(model)
            names.append("%s:templates/%s%s%s" % (package, model.__tablename__,
                                                   self.template_name_suffix, self.template_extension))

        # For benefit of tests
        self._template_names = names

        return names
Ejemplo n.º 2
0
 def get_context_object_name(self, object_list):
     """
     Get the name of the item to be used in the context.
     """
     if self.context_object_name:
         return self.context_object_name
     elif isinstance(object_list, Query):
         return '%s_list' % utils.model_from_query(object_list).__tablename__
     else:
         return None
Ejemplo n.º 3
0
    def get_object(self, query=None):
        """
        Returns the object the view is displaying.

        By default this requires `self.query` and a `pk` or `slug` argument
        in the URLconf, but subclasses can override this to return any object.
        """
        # Use a custom query if provided; this is required for subclasses
        # like DateDetailView
        if query is None:
            query = self.get_query()

        # Next, try looking up by primary key.
        pk = self.kwargs.get(self.pk_url_kwarg, None)
        slug = self.kwargs.get(self.slug_url_kwarg, None)
        if pk is not None:
            pk_field = utils.get_pk_field(query)
            query = query.filter(pk_field==pk)

        # Next, try looking up by slug.
        elif slug is not None:
            slug_field = utils.get_field(query, self.get_slug_field())
            query = query.filter(slug_field==slug)

        # If none of those are defined, it's an error.
        else:
            raise AttributeError("Generic detail view %s must be called with "
                                 "either an object pk or a slug."
                                 % self.__class__.__name__)

        try:
            # Get the single item from the filtered query
            obj = query.one()
        except NoResultFound:
            raise httpexceptions.HTTPNotFound(_("No %(verbose_name)s found matching the query") %
                                            {'verbose_name': utils.model_from_query(query).__name__})
        return obj