zoukankan      html  css  js  c++  java
  • django 中的render和render_to_response()和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())

     

  • 相关阅读:
    discuz $_G变量
    php & 引用
    discuz模板引擎
    nginx正则表达式
    linux系统安装 dig和nslookup命令
    linux中的各种$号 位置参数变量
    memcache图形管理工具
    mysql 注入问题
    pycharm 连接mysql失败
    Bootstrap 常用网站
  • 原文地址:https://www.cnblogs.com/wangchaowei/p/6750512.html
Copyright © 2011-2022 走看看