• render, render_to_response, redirect,


    自django1.3开始:render()方法是render_to_response的一个崭新的快捷方式,前者会自动使用RequestContext。而后者必须coding出来,这是最明显的区别,当然前者更简洁。

        return render_to_response('blog_add.html',{'blog': blog, 'form': form, 'id': id, 'tag': tag},
                                  context_instance=RequestContext(request))

        return render(request, 'blog_add.html', {'blog': blog, 'form': form, 'id': id, 'tag': tag})

    locals()用法:locals()可以直接将函数中所有的变量全部传给模板。当然这可能会传递一些多余的参数,有点浪费内存的嫌疑。
        return render(request, 'blog_add.html',locals())

    --------------------------------------------------------------------------------------------------------

    1. django中的render

    context在Django里表现为 Context 类,在 django.template 模块里。 它的构造函数带有一个可选的参数:

    一个字典映射变量和它们的值。 调用 Template 对象 的 render() 方法并传递context来填充模板:

    >>> from django.template import Context, Template  
    >>> t = Template('My name is {{ name }}.')  
    >>> c = Context({'name': 'Stephane'})  
    >>> t.render(c)  
    u'My name is Stephane.'

    在views.py中:

    return render(request, 'blog_add.html', {'blog': blog, 'form': form, 'id': id, 'tag': tag})

    2. django中的render_to_response

     return render_to_response('blog_add.html', {'blog': blog, 'form': form, 'id': id, 'tag': tag})

    很明显,如果使用render_to_response就省去了render里传递的request。

    3.locals()用法:locals()可以直接将函数中所有的变量全部传给模板。当然这可能会传递一些多余的参数,有点浪费内存的嫌疑。

    return render(request, 'blog_add.html', locals())
    return render_to_response('blog_add.html', locals())


  • 相关阅读:
    转换相关内置函数_hex
    转换相关内置函数_oct
    内置函数help
    内置函数_locals_变量相关
    内置函数_globals_变量相关
    智慧工地支撑平台工地一张图在线管控平台
    轨道交通GIS平台的应用分析
    基于BIM+GIS钢结构全生命周期管理平台项目
    应急指挥中心系统的研究与设计
    基于SuperMap10i 开发的全过程咨询管理平台的研究
  • 原文地址:https://www.cnblogs.com/fengff/p/8124424.html
Copyright © 2020-2023  润新知