<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 />
阅读(4941) | 评论(0) | 转发(0) |