Chinaunix首页 | 论坛 | 博客
  • 博客访问: 657476
  • 博文数量: 87
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2022
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-23 11:16
个人简介

西邮大三狗!!!

文章分类

全部博文(87)

文章存档

2015年(47)

2014年(40)

分类: Python/Ruby

2015-06-17 22:29:07

<span style="font-size:14px;">最近在学习python的Django框架,在学习使用模板的时候发现render_to_response()这个方法挺有意思的,记录一下</span><img src="http://blog.chinaunix.net/kindeditor/plugins/emoticons/images/0.gif" border="0" alt="" /><br /> <span style="font-size:14px;">看下面两个视图函数:</span><br /> <div id="codeText" class="codeText"> <ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"> <li> <span style="color:#000000;"><span style="color:#0000FF;">from</span> django<span style="color:#0000CC;">.</span>template <span style="color:#0000FF;">import</span> loader<span style="color:#0000CC;">,</span>Context<br /> </span> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">def</span> view_1<span style="color:#0000CC;">(</span>req<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li>     t <span style="color:#0000CC;">=</span> loader<span style="color:#0000CC;">.</span>get_template<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'index1.html'</span><span style="color:#0000CC;">)</span><br /> </li> <li>     c <span style="color:#0000CC;">=</span> Context<span style="color:#0000CC;">(</span><br /> </li> <li>             <span style="color:#0000CC;">{</span><span style="color:#FF00FF;">'app'</span><span style="color:#0000CC;">:</span><span style="color:#FF00FF;">'My app'</span><span style="color:#0000CC;">,</span><span style="color:#FF00FF;">'user'</span><span style="color:#0000CC;">:</span>req<span style="color:#0000CC;">.</span>user<span style="color:#0000CC;">,</span><span style="color:#FF00FF;">'message'</span><span style="color:#0000CC;">:</span><span style="color:#FF00FF;">'func view 1'</span><span style="color:#0000CC;">}</span><br /> </li> <li>             <span style="color:#0000CC;">)</span><br /> </li> <li>     <span style="color:#0000FF;">return</span> t<span style="color:#0000CC;">.</span>render<span style="color:#0000CC;">(</span>c<span style="color:#0000CC;">)</span><br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">def</span> view_2<span style="color:#0000CC;">(</span>req<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li>     t <span style="color:#0000CC;">=</span> loader<span style="color:#0000CC;">.</span>get_template<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'index2.html'</span><span style="color:#0000CC;">)</span><br /> </li> <li>     c <span style="color:#0000CC;">=</span> Context<span style="color:#0000CC;">(</span><br /> </li> <li>             <span style="color:#0000CC;">{</span><span style="color:#FF00FF;">'app'</span><span style="color:#0000CC;">:</span><span style="color:#FF00FF;">'My app'</span><span style="color:#0000CC;">,</span><span style="color:#FF00FF;">'user'</span><span style="color:#0000CC;">:</span>req<span style="color:#0000CC;">.</span>user<span style="color:#0000CC;">,</span><span style="color:#FF00FF;">'message'</span><span style="color:#0000CC;">:</span><span style="color:#FF00FF;">'func view 2'</span><span style="color:#0000CC;">}</span><br /> </li> <li>             <span style="color:#0000CC;">)</span><br /> </li> <li>     <span style="color:#0000FF;">return</span> t<span style="color:#0000CC;">.</span>render<span style="color:#0000CC;">(</span>c<span style="color:#0000CC;">)</span> </li> </ol> </div> <span style="font-size:14px;">这里不使用render_to_response()方法,而是选择使用手动载入模板。上面代码的共同点是非常明显的(app,user两个变量是相同的)。能不能抽取共性呢?</span><span style="font-size:14px;"></span><br /> <span style="font-size:14px;">我们可以使用RequestContext和context处理器解决上述问题,Just like:</span><br /> <div id="codeText" class="codeText"> <ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"> <li> <span style="color:#000000;"><span style="color:#0000FF;">from</span> django<span style="color:#0000CC;">.</span>shortcuts <span style="color:#0000FF;">import</span> render_to_response<br /> </span> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">def</span> the_same<span style="color:#0000CC;">(</span>req<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li>     <span style="color:#0000FF;">return</span> <span style="color:#0000CC;">{</span><span style="color:#FF00FF;">'app'</span><span style="color:#0000CC;">:</span><span style="color:#FF00FF;">'My app'</span><span style="color:#0000CC;">,</span><span style="color:#FF00FF;">'user'</span><span style="color:#0000CC;">:</span>req<span style="color:#0000CC;">.</span>user<span style="color:#0000CC;">}</span><br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">def</span> view_1<span style="color:#0000CC;">(</span>req<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li>     <span style="color:#0000FF;">return</span> render_to_response<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'index1.html'</span><span style="color:#0000CC;">,</span><span style="color:#0000CC;">{</span><span style="color:#FF00FF;">'message'</span><span style="color:#0000CC;">:</span><span style="color:#FF00FF;">'func view 1'</span><span style="color:#0000CC;">}</span><span style="color:#0000CC;">,</span>context_instance<span style="color:#0000CC;">=</span>RequestContext<span style="color:#0000CC;">(</span>req<span style="color:#0000CC;">,</span>processors<span style="color:#0000CC;">=</span><span style="color:#0000CC;">[</span>the_same<span style="color:#0000CC;">]</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">def</span> view_2<span style="color:#0000CC;">(</span>req<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li>     <span style="color:#0000FF;">return</span> render_to_response<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'index2.html'</span><span style="color:#0000CC;">,</span><span style="color:#0000CC;">{</span><span style="color:#FF00FF;">'message'</span><span style="color:#0000CC;">:</span><span style="color:#FF00FF;">'func view 2'</span><span style="color:#0000CC;">}</span><span style="color:#0000CC;">,</span>context_instance<span style="color:#0000CC;">=</span>RequestContext<span style="color:#0000CC;">(</span>req<span style="color:#0000CC;">,</span>processors<span style="color:#0000CC;">=</span><span style="color:#0000CC;">[</span>the_same<span style="color:#0000CC;">]</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span> </li> </ol> </div> <span style="font-size:14px;">咋一看,render_to_response()方法的参数蛮有意思的,不急,先了解下这段代码是怎么个意思:</span><br /> <span style="font-size:14px;">先看the_same函数,接收</span><span style="font-size:14px;">一个HttpResponse对象,然后返回一个字典;接下来的两个函数中,用RequestContext代替了之前的Context。而这个RequestContext有两个参数,一个是HttpResponse,另一个是可选参数processors</span><span style="font-size:14px;">(是一个包含context处理器的列表或函数</span><span style="font-size:14px;">)。每个视图函数中不再包含app,user变量,这些直接由the_same函数提供。</span><br /> <span style="font-size:14px;"></span><br /> <span style="font-size:14px;">再来</span><span style="font-size:14px;">看一下render_to_response()方法的原型:</span><br /> <tt class="descname" style="line-height:13.1999998092651px;font-size:13.1999998092651px;font-weight:bold;white-space:normal;background-color:#FFFFFF;">render_to_response</tt><span style="font-family:Verdana, sans-serif;font-size:13.1999998092651px;font-weight:bold;line-height:19.7999992370605px;white-space:normal;background-color:#FFFFFF;">(</span><em style="font-family:Verdana, sans-serif;font-size:13.1999998092651px;line-height:19.7999992370605px;white-space:normal;background-color:#FFFFFF;">template_name[, dictionary][, context_instance][, content_type]</em><span style="font-family:Verdana, sans-serif;font-size:13.1999998092651px;font-weight:bold;line-height:19.7999992370605px;white-space:normal;background-color:#FFFFFF;">)</span><br /> <span style="font-size:14px;">下面是Django文档中对该方法的解释</span><br /> <div id="codeText" class="codeText"> <ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"> <li> Required arguments </li> <li> <strong><br /> </strong> </li> <li> <strong>template_name</strong> </li> <li> The full name of a template to use or sequence of template names. If a sequence is given, the first template that exists will be used. See the template loader documentation for more information on how templates are found. </li> <li> Optional arguments </li> <li> <strong><br /> </strong> </li> <li> <strong>dictionary</strong> </li> <li> A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template. </li> <li> <strong><br /> </strong> </li> <li> <strong>context_instance</strong> </li> <li> The context instance to render the template with. By default, the template will be rendered with a Context instance (filled with values from dictionary). If you need to use context processors, render the template with a RequestContext instance instead.  </li> <li> <br /> </li> <li> <strong>content_type</strong> </li> <li> The MIME type to use for the resulting document. Defaults to the value of the DEFAULT_CONTENT_TYPE setting. </li> </ol> </div> <span style="font-size:14px;">刚开始用这个方法的时候,我只用到了template_name、dictionary两个参数,关于context_instance几乎没用过,怎么用(上面的英文写的很明白了,我也就不翻译了</span><img src="http://blog.chinaunix.net/kindeditor/plugins/emoticons/images/13.gif" border="0" alt="" /><span style="font-size:14px;">)</span><br /> <br />
阅读(5010) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~