美文网首页
template response

template response

作者: xncode | 来源:发表于2017-05-13 17:10 被阅读0次

    源代码

    from django.http import HttpResponse
    from django.utils import six
    
    from .loader import get_template, select_template
    
    
    class SimpleTemplateResponse(HttpResponse):
        rendering_attrs = ['template_name', 'context_data', '_post_render_callbacks']
    
        def __init__(self, template, context=None, content_type=None, status=None,
                    charset=None, using=None):
        
            self.template_name = template
            self.context_data = context
            self.using = using
    
            self._post_render_callbacks = []
            self._request = None
            super(SimpleTemplateResponse, self).__init__('', content_type, status, charset=charset)
            self._is_rendered = False
    
        def resolve_template(self, template):
            # 获取模板
            if isinstance(template, (list, tuple)):
                return select_template(template, using=self.using)
            elif isinstance(template, six.string_types):
                return get_template(template, using=self.using)
            else:
                return template
        def resolve_context(self, context):
            # 获取context
            return context
        @property
        def rendered_content(self):
            # 和我在发送邮件时的处理一样
            template = self.resolve_template(self.template_name)
            context = self.resolve_context(self.context_data)
            content = template.render(context, self._request)
            return content
    
        def add_post_render_callback(self, callback):
    
            if self._is_rendered:
                callback(self)
            else:
                self._post_render_callbacks.append(callback)
    
        def render(self):
            
            retval = self
            if not self._is_rendered:
                self.content = self.rendered_content
    
                # 调用之前注册的callback
                for post_callback in self._post_render_callbacks:
                    newretval = post_callback(retval)
                    if newretval is not None:
                        retval = newretval
    
            return retval
    
        @property
        def is_rendered(self):
            return self._is_rendered
    
        def __iter__(self):
            if not self._is_rendered:
                raise ContentNotRenderedError(
                    'The response content must be rendered before it can be iterated over.'
                )
            return super(SimpleTemplateResponse, self).__iter__()
    
        @property
        def content(self):
            if not self._is_rendered:
                raise ContentNotRenderedError(
                    'The response content must be rendered before it can be accessed.'
                )
            return super(SimpleTemplateResponse, self).content
    
        @content.setter
        def content(self, value):
            HttpResponse.content.fset(self, value)
            self._is_rendered = True
    
    
    class TemplateResponse(SimpleTemplateResponse):
        rendering_attrs = SimpleTemplateResponse.rendering_attrs + ['_request']
    
        def __init__(self, request, template, context=None, content_type=None,
                    status=None, charset=None, using=None):
            super(TemplateResponse, self).__init__(
                template, context, content_type, status, charset, using)
            self._request = request

    相关文章

      网友评论

          本文标题:template response

          本文链接:https://www.haomeiwen.com/subject/zqedtxtx.html