【django】django render()和render_to_response()和direct_to_template()和locals()
2016-12-14 19:08
706 查看
转载自:https://www.douban.com/note/278152737/
前两者区别stackoverflow给了较明确的答案
参考网址:http://stackoverflow.com/questions/5154358/django-what-is-the-difference-between-render-render-to-response-and-direc
自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})
stackoverflow里还给了return direct_to_template(request, template_name, my_data_dictionary)这样一个例子,关于其解释:
direct_to_template() is designed to be used just in urls.py without a view defined in views.py but it can be used in views.py to avoid having to type RequestContext.
direct_to_template is something different. It's a generic view that uses a data dictionary to render the html without the need of the views.py, you use it in urls.py.
direct_to_template()还没用到过,不再赘述,留下此方法,备用
locals()用法:locals()可以直接将函数中所有的变量全部传给模板。当然这可能会传递一些多余的参数,有点浪费内存的嫌疑,对可读性有影响,不推荐。
return render(request, 'blog_add.html',locals())
那么长一段代码精简到三分之一的长度。有种爽歪歪的感觉!feel well !
前两者区别stackoverflow给了较明确的答案
参考网址:http://stackoverflow.com/questions/5154358/django-what-is-the-difference-between-render-render-to-response-and-direc
自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})
stackoverflow里还给了return direct_to_template(request, template_name, my_data_dictionary)这样一个例子,关于其解释:
direct_to_template() is designed to be used just in urls.py without a view defined in views.py but it can be used in views.py to avoid having to type RequestContext.
direct_to_template is something different. It's a generic view that uses a data dictionary to render the html without the need of the views.py, you use it in urls.py.
direct_to_template()还没用到过,不再赘述,留下此方法,备用
locals()用法:locals()可以直接将函数中所有的变量全部传给模板。当然这可能会传递一些多余的参数,有点浪费内存的嫌疑,对可读性有影响,不推荐。
return render(request, 'blog_add.html',locals())
那么长一段代码精简到三分之一的长度。有种爽歪歪的感觉!feel well !
相关文章推荐
- django 中的render和render_to_response()和locals()
- django之render_to_response()
- django render_to_response的使用
- Django的render_to_response返回的变量转义问题解决
- [Django模板系统]用render_to_response()简化views层
- 对django中render()与render_to_response()的区别详解
- django中render()与render_to_response()区别
- Django中render和render_to_response的区别
- django之render_to_response()
- Django中render_to_response和render的区别(转载)
- Django框架中render_to_response()函数的使用方法
- django render和render_to_response()
- Django框架中render_to_response()函数的使用方法
- <django中render_to_response的可选参数和使用方法>
- Django中提示render_to_response未定义的错误
- django之render_to_response()
- render_to_response()改进HttPResponse
- djanjo中render,render_to_response 两者的区别
- 用render_to_response()简化views层
- Django中视图总结[urls匹配,HttpRequest对象,HttpResponse,render,redirect对象,对象序列化接受及案例]