Ejemplo n.º 1
0
    def get_template_names(self):
        """
        Return a list of template names to be used for the request. May not be
        called if render_to_response is overridden. Returns the following list:

        * the value of ``template_name`` on the view (if provided)
        * the contents of the ``template_name_field`` field on the
          object instance that the view is operating upon (if available)
        * ``<app_label>/<model_name><template_name_suffix>.html``
        """
        try:
            names = super(SingleObjectTemplateResponseMixin, 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 self.template_name_field is set, grab the value of the field
            # of that name from the object; this is the most specific template
            # name, if given.
            if self.object and self.template_name_field:
                name = getattr(self.object, self.template_name_field, None)
                if name:
                    names.insert(0, name)

            # The least-specific option is the default <app>/<model>_detail.html;
            # only use this if the object in question is a model.
            if hasattr(self.object, '__table__'):
                template_package = utils.get_template_package_name(self.object)
                names.append("%s:templates/%s%s%s" % (
                    template_package,
                    self.object.__tablename__,
                    self.template_name_suffix,
                    self.template_extension
                ))
            elif hasattr(self, 'model') and self.model is not None and hasattr(self.model, '__tablename__'):
                template_package = utils.get_template_package_name(self.model)
                names.append("%s:templates/%s%s%s" % (
                    template_package,
                    self.model.__tablename__,
                    self.template_name_suffix,
                    self.template_extension
                ))

            # If we still haven't managed to find any template names, we should
            # re-raise the ImproperlyConfigured to alert the user.
            if not names:
                raise

            # For benefit of tests
            self._template_names = names

        return names
Ejemplo n.º 2
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