middleware.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:django-ajax-views 作者: Pyco7 项目源码 文件源码
def process_response(self, request, response):
        if not request.is_ajax() and not isinstance(response, HttpResponseRedirect) and hasattr(response, 'content'):
            content = force_text(response.content, encoding=response.charset)
            if '</body>' not in content:
                return response

            json_cfg = {}
            if hasattr(response, 'context_data'):
                json_cfg = response.context_data.get('json_cfg', {})

            template = get_template('ajaxviews/_middleware.html')
            html = template.render({
                'json_cfg': json_cfg,
                'main_name': settings.REQUIRE_MAIN_NAME,
            })

            l_content, r_content = content.rsplit('</body>', 1)
            content = ''.join([l_content, html, '</body>', r_content])

            response.content = response.make_bytes(content)
            if response.get('Content-Length', None):
                response['Content-Length'] = len(response.content)
        return response
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号