在django文档中render()函数的作用是载入模板,填充上下文,再返回由它生成的 HttpResponse
对象
正常载入模版时要导入loader和HttpResponse,如果你使用了render()就不用导入前两者了,但如果你还有其他函数需要用到它的话,就需要保持HttpResponse的导入
help文档中描述如下:
render(request, template_name, context=None, content_type=None, status=None, using=None)
Returns a HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments.
此方法的作用---结合一个给定的模板和一个给定的上下文字典,并返回一个渲染后的 HttpResponse 对象。
通俗的讲就是把context的内容, 加载进templates中定义的文件, 并通过浏览器渲染呈现.
参数讲解:
request: 是一个固定参数, 没什么好讲的。
template_name: templates 中定义的文件, 要注意路径名. 比如'templates\polls\index.html', 参数就要写‘polls\index.html’
context: 要传入文件中用于渲染呈现的数据, 默认是字典格式
content_type: 生成的文档要使用的MIME 类型。默认为DEFAULT_CONTENT_TYPE 设置的值。
status: http的响应代码,默认是200.
using: 用于加载模板使用的模板引擎的名称。