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,通过模板引擎来获取模板对象。
原文地址:https://www.cnblogs.com/baishoujing/p/7217763.html