{"id":727,"date":"2017-04-22T14:11:45","date_gmt":"2017-04-22T06:11:45","guid":{"rendered":"http:\/\/192.168.1.20\/?p=727"},"modified":"2017-10-23T11:52:48","modified_gmt":"2017-10-23T03:52:48","slug":"django-render{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}e5{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}92{47dd41ec6bdcbd9a214c783a3735392b82ea6d1d918378c5a5","status":"publish","type":"post","link":"http:\/\/alextop.top\/?p=727","title":{"rendered":"django render()\u548crender_to_response()\u548cdirect_to_template() \u548clocals()"},"content":{"rendered":"<p style=\"background: white\"><span style=\"color:#111111; font-family:Helvetica; font-size:9pt\">\u00a0<span style=\"color:#999999\">2013-05-23 12:18:56<span style=\"color:#111111\"><br \/>\n\t\t\t\t<\/span><\/span><\/span><\/p>\n<p style=\"background: white\"><span style=\"color:#111111; font-size:10pt\"><span style=\"font-family:\u5b8b\u4f53\">\u524d\u4e24\u8005\u533a\u522b<\/span><span style=\"font-family:Arial\">stackoverflow<\/span><span style=\"font-family:\u5b8b\u4f53\">\u7ed9\u4e86\u8f83\u660e\u786e\u7684\u7b54\u6848<\/span><span style=\"font-family:Arial\"><br \/><\/span><span style=\"font-family:\u5b8b\u4f53\">\u53c2\u8003\u7f51\u5740\uff1a<a href=\"http:\/\/stackoverflow.com\/questions\/5154358\/django-what-is-the-difference-between-render-render-to-response-and-direc\" target=\"_blank\"\/><\/span><span style=\"color:#666699\"><span style=\"font-family:Arial\">http:\/\/stackoverflow.com\/questions\/5154358\/django-what-is-the-difference-between-render-render-to-response-and-direc<span style=\"color:#111111\"><\/p>\n<p><\/span><\/span><span style=\"font-family:\u5b8b\u4f53\">\u81ea<\/span><span style=\"color:#111111\"><span style=\"font-family:Arial\">django1.3<\/span><span style=\"font-family:\u5b8b\u4f53\">\u5f00\u59cb\uff1a<\/span><span style=\"font-family:Arial\">render()<\/span><span style=\"font-family:\u5b8b\u4f53\">\u65b9\u6cd5\u662f<\/span><span style=\"font-family:Arial\">render_to_response<\/span><span style=\"font-family:\u5b8b\u4f53\">\u7684\u4e00\u4e2a\u5d2d\u65b0\u7684\u5feb\u6377\u65b9\u5f0f\uff0c\u524d\u8005\u4f1a\u81ea\u52a8\u4f7f\u7528<\/span><span style=\"font-family:Arial\">RequestContext<\/span><span style=\"font-family:\u5b8b\u4f53\">\u3002\u800c\u540e\u8005\u5fc5\u987b<\/span><span style=\"font-family:Arial\">coding<\/span><span style=\"font-family:\u5b8b\u4f53\">\u51fa\u6765\uff0c\u8fd9\u662f\u6700\u660e\u663e\u7684\u533a\u522b\uff0c\u5f53\u7136\u524d\u8005\u66f4\u7b80\u6d01\u3002<\/span><span style=\"font-family:Arial\"><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0return render_to_response(&#8216;blog_add.html&#8217;,{&#8216;blog&#8217;: blog, &#8216;form&#8217;: form, &#8216;id&#8217;: id, &#8216;tag&#8217;: tag},<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0context_instance=RequestContext(request))<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0return render(request, &#8216;blog_add.html&#8217;, {&#8216;blog&#8217;: blog, &#8216;form&#8217;: form, &#8216;id&#8217;: id, &#8216;tag&#8217;: tag})<\/p>\n<p>stackoverflow<\/span><span style=\"font-family:\u5b8b\u4f53\">\u91cc\u8fd8\u7ed9\u4e86<\/span><span style=\"font-family:Arial\">return direct_to_template(request, template_name, my_data_dictionary)<\/span><span style=\"font-family:\u5b8b\u4f53\">\u8fd9\u6837\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5173\u4e8e\u5176\u89e3\u91ca\uff1a<\/span><span style=\"font-family:Arial\"><br \/>\u00a0\u00a0\u00a0\u00a0\u00a0direct_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.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0direct_to_template is something different. It&#8217;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. <\/p>\n<p>direct_to_template()<\/span><span style=\"font-family:\u5b8b\u4f53\">\u8fd8\u6ca1\u7528\u5230\u8fc7\uff0c\u4e0d\u518d\u8d58\u8ff0\uff0c\u7559\u4e0b\u6b64\u65b9\u6cd5\uff0c\u5907\u7528<\/span><span style=\"font-family:Arial\"><\/p>\n<p>locals()<\/span><span style=\"font-family:\u5b8b\u4f53\">\u7528\u6cd5\uff1a<\/span><span style=\"font-family:Arial\">locals()<\/span><span style=\"font-family:\u5b8b\u4f53\">\u53ef\u4ee5\u76f4\u63a5\u5c06\u51fd\u6570\u4e2d\u6240\u6709\u7684\u53d8\u91cf\u5168\u90e8\u4f20\u7ed9\u6a21\u677f\u3002\u5f53\u7136\u8fd9\u53ef\u80fd\u4f1a\u4f20\u9012\u4e00\u4e9b\u591a\u4f59\u7684\u53c2\u6570\uff0c\u6709\u70b9\u6d6a\u8d39\u5185\u5b58\u7684\u5acc\u7591\uff0c\u5bf9\u53ef\u8bfb\u6027\u6709\u5f71\u54cd\uff0c\u4e0d\u63a8\u8350\u3002<\/span><span style=\"font-family:Arial\"><br \/>\u00a0\u00a0\u00a0\u00a0return render(request, &#8216;blog_add.html&#8217;,locals())<\/p>\n<p><\/span><span style=\"font-family:\u5b8b\u4f53\">\u90a3\u4e48\u957f\u4e00\u6bb5\u4ee3\u7801\u7cbe\u7b80\u5230\u4e09\u5206\u4e4b\u4e00\u7684\u957f\u5ea6\u3002\u6709\u79cd\u723d\u6b6a\u6b6a\u7684\u611f\u89c9\uff01<\/span><span style=\"font-family:Arial\">feel well !<br \/>\n<\/span><\/span><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a02013-05-23 12:&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,10],"tags":[],"class_list":["post-727","post","type-post","status-publish","format-standard","hentry","category-python","category-jishu"],"_links":{"self":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/727"}],"collection":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=727"}],"version-history":[{"count":1,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/727\/revisions"}],"predecessor-version":[{"id":986,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/727\/revisions\/986"}],"wp:attachment":[{"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}