zoukankan      html  css  js  c++  java
  • Django render函数

    render()

    此方法的作用---结合一个给定的模板和一个给定的上下文字典,并返回一个渲染后的 HttpResponse 对象。

    通俗的讲就是把context的内容, 加载进templates中定义的文件, 并通过浏览器渲染呈现.

    简单示例:

    hello.html

    <h1>{{hello }}</h1>
    #views.py
    from django.shortcuts import render  
       
    def hello(request):  
        context = {}  
        context['hello'] = 'Hello World!'  
        return render(request, 'hello.html', context)  
       #return render(request, 'hello.html', {'hello':'Hello World!'})

    render()函数传递context来填充模板

    help文档中render描述

    render(request, template_name, context=None, content_type=None, status=None, using=None)

    参数:

    request: 是一个固定参数

    template_name: templates中定义的文件,注意路径名。比如:"templates/polls/index.html", 则参数这样写:"polls/index.html"

    context: 要传入文件中用于渲染呈现的数据, 默认是字典格式

    content_type: 生成的文档要使用的MIME 类型。默认为DEFAULT_CONTENT_TYPE 设置的值。

    status: http的响应代码,默认是200.

    using: 用于加载模板使用的模板引擎的名称。

  • 相关阅读:
    软件性能测试知识汇总
    软件功能测试知识汇总
    机器学习——KNN算法(k近邻算法)
    Shell脚本语法
    机器学习环境搭建及基础
    shell基础及变量
    查准率和召回率理解
    python中的矩阵、多维数组
    链表:反转链表
    栈和队列:生成窗口最大值数组
  • 原文地址:https://www.cnblogs.com/dear_diary/p/8327839.html
Copyright © 2011-2022 走看看