zoukankan      html  css  js  c++  java
  • Django源码学习 了解render与render_to_response

    render与render_to_response
    def render_to_response(template_name, context=None, content_type=None, status=None, using=None):
        content = loader.render_to_string(template_name, context, using=using)
        return HttpResponse(content, content_type, status)
    def render(request, template_name, context=None, content_type=None, status=None, using=None):
        content = loader.render_to_string(template_name, context, request, using=using)
        return HttpResponse(content, content_type, status)
    这两个快捷函数都是内部调用了django.template.loader.render_to_string函数.
    返回的HTML内容文本给response。
    这两个函数之间的区别就在于是否传入request对象,其实传入的context上下文最终传入render_to_string中,然后获取选择模板对象,然后模板对象调用其render函数,再次传入context上下文对象,插入模板返回一系列HTML文本字符串。
    最后这些字符串向外传递给(feed into)HTTPResponse对象,快捷返回的是HTTPResponse对象。
    render_to_string中有两个重要的函数被调用就是get_template和select_template,通过模板引擎来获取模板对象。
  • 相关阅读:
    第6周编程题:零基础学Java
    帆软报表软件学习计划
    北大软件工程——第八周:面向对象设计2
    hdu1264 Counting Squares
    hdu1264 Counting Squares
    poj1151 Atlantis(线段树+扫描线)
    poj1151 Atlantis(线段树+扫描线)
    bzoj4653 [Noi2016]区间
    bzoj4653 [Noi2016]区间
    Tyvj1043
  • 原文地址:https://www.cnblogs.com/baishoujing/p/7217763.html
Copyright © 2011-2022 走看看