/usr/share/doc/python-django-braces/html/other.html is in python-django-braces 1.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Other Mixins — django-braces 1.3.1 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.3.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="django-braces 1.3.1 documentation" href="index.html" />
<link rel="prev" title="Form Mixins" href="form.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="form.html" title="Form Mixins"
accesskey="P">previous</a> |</li>
<li><a href="index.html">django-braces 1.3.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="other-mixins">
<h1><a class="toc-backref" href="#id9">Other Mixins</a><a class="headerlink" href="#other-mixins" title="Permalink to this headline">¶</a></h1>
<p>These mixins handle other random bits of Django’s views, like controlling output, controlling content types, or setting values in the context.</p>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#other-mixins" id="id9">Other Mixins</a><ul>
<li><a class="reference internal" href="#setheadlinemixin" id="id10">SetHeadlineMixin</a><ul>
<li><a class="reference internal" href="#static-example" id="id11">Static Example</a></li>
<li><a class="reference internal" href="#dynamic-example" id="id12">Dynamic Example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#selectrelatedmixin" id="id13">SelectRelatedMixin</a></li>
<li><a class="reference internal" href="#prefetchrelatedmixin" id="id14">PrefetchRelatedMixin</a></li>
<li><a class="reference internal" href="#jsonresponsemixin" id="id15">JSONResponseMixin</a></li>
<li><a class="reference internal" href="#jsonrequestresponsemixin" id="id16">JsonRequestResponseMixin</a></li>
<li><a class="reference internal" href="#ajaxresponsemixin" id="id17">AjaxResponseMixin</a></li>
<li><a class="reference internal" href="#orderablelistmixin" id="id18">OrderableListMixin</a></li>
<li><a class="reference internal" href="#canonicalslugdetailmixin" id="id19">CanonicalSlugDetailMixin</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="setheadlinemixin">
<span id="id1"></span><h2><a class="toc-backref" href="#id10">SetHeadlineMixin</a><a class="headerlink" href="#setheadlinemixin" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">SetHeadlineMixin</span></tt> is a newer edition to our client’s CMS. It allows us to <em>statically</em> or <em>programmatically</em> set the headline of any of our views. We like to write as few templates as possible, so a mixin like this helps us reuse generic templates. Its usage is amazingly straightforward and works much like Django’s built-in <tt class="docutils literal"><span class="pre">get_queryset</span></tt> method. This mixin has two ways of being used.</p>
<div class="section" id="static-example">
<h3><a class="toc-backref" href="#id11">Static Example</a><a class="headerlink" href="#static-example" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">SetHeadlineMixin</span>
<span class="k">class</span> <span class="nc">HeadlineView</span><span class="p">(</span><span class="n">SetHeadlineMixin</span><span class="p">,</span> <span class="n">TemplateView</span><span class="p">):</span>
<span class="n">headline</span> <span class="o">=</span> <span class="s">"This is our headline"</span>
<span class="n">template_name</span> <span class="o">=</span> <span class="s">"path/to/template.html"</span>
</pre></div>
</div>
</div>
<div class="section" id="dynamic-example">
<h3><a class="toc-backref" href="#id12">Dynamic Example</a><a class="headerlink" href="#dynamic-example" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
<span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">SetHeadlineMixin</span>
<span class="k">class</span> <span class="nc">HeadlineView</span><span class="p">(</span><span class="n">SetHeadlineMixin</span><span class="p">,</span> <span class="n">TemplateView</span><span class="p">):</span>
<span class="n">template_name</span> <span class="o">=</span> <span class="s">"path/to/template.html"</span>
<span class="k">def</span> <span class="nf">get_headline</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s">u"This is our headline for </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
</pre></div>
</div>
<p>In both usages, in the template, just print out <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">headline</span> <span class="pre">}}</span></tt> to show the generated headline.</p>
</div>
</div>
<div class="section" id="selectrelatedmixin">
<span id="id2"></span><h2><a class="toc-backref" href="#id13">SelectRelatedMixin</a><a class="headerlink" href="#selectrelatedmixin" title="Permalink to this headline">¶</a></h2>
<p>A simple mixin which allows you to specify a list or tuple of foreign key fields to perform a <a class="reference external" href="https://docs.djangoproject.com/en/1.5/ref/models/querysets/#select-related">select_related</a> on. See Django’s docs for more information on <a class="reference external" href="https://docs.djangoproject.com/en/1.5/ref/models/querysets/#select-related">select_related</a>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># views.py</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">DetailView</span>
<span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">SelectRelatedMixin</span>
<span class="kn">from</span> <span class="nn">profiles.models</span> <span class="kn">import</span> <span class="n">Profile</span>
<span class="k">class</span> <span class="nc">UserProfileView</span><span class="p">(</span><span class="n">SelectRelatedMixin</span><span class="p">,</span> <span class="n">DetailView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Profile</span>
<span class="n">select_related</span> <span class="o">=</span> <span class="p">[</span><span class="s">"user"</span><span class="p">]</span>
<span class="n">template_name</span> <span class="o">=</span> <span class="s">"profiles/detail.html"</span>
</pre></div>
</div>
</div>
<div class="section" id="prefetchrelatedmixin">
<span id="id3"></span><h2><a class="toc-backref" href="#id14">PrefetchRelatedMixin</a><a class="headerlink" href="#prefetchrelatedmixin" title="Permalink to this headline">¶</a></h2>
<p>A simple mixin which allows you to specify a list or tuple of reverse foreign key or ManyToMany fields to perform a <a class="reference external" href="https://docs.djangoproject.com/en/1.5/ref/models/querysets/#prefetch-related">prefetch_related</a> on. See Django’s docs for more information on <a class="reference external" href="https://docs.djangoproject.com/en/1.5/ref/models/querysets/#prefetch-related">prefetch_related</a>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># views.py</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">DetailView</span>
<span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">PrefetchRelatedMixin</span>
<span class="k">class</span> <span class="nc">UserView</span><span class="p">(</span><span class="n">PrefetchRelatedMixin</span><span class="p">,</span> <span class="n">DetailView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">User</span>
<span class="n">prefetch_related</span> <span class="o">=</span> <span class="p">[</span><span class="s">"post_set"</span><span class="p">]</span> <span class="c"># where the Post model has an FK to the User model as an author.</span>
<span class="n">template_name</span> <span class="o">=</span> <span class="s">"users/detail.html"</span>
</pre></div>
</div>
</div>
<div class="section" id="jsonresponsemixin">
<span id="id4"></span><h2><a class="toc-backref" href="#id15">JSONResponseMixin</a><a class="headerlink" href="#jsonresponsemixin" title="Permalink to this headline">¶</a></h2>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.1: </span><tt class="docutils literal"><span class="pre">render_json_response</span></tt> now accepts a <tt class="docutils literal"><span class="pre">status_code</span></tt> keyword argument.
<tt class="docutils literal"><span class="pre">json_dumps_kwargs</span></tt> class-attribute and <tt class="docutils literal"><span class="pre">get_json_dumps_kwargs</span></tt> method to provide arguments to the <tt class="docutils literal"><span class="pre">json.dumps()</span></tt> method.</p>
</div>
<p>A simple mixin to handle very simple serialization as a response to the browser.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># views.py</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">DetailView</span>
<span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">JSONResponseMixin</span>
<span class="k">class</span> <span class="nc">UserProfileAJAXView</span><span class="p">(</span><span class="n">JSONResponseMixin</span><span class="p">,</span> <span class="n">DetailView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Profile</span>
<span class="n">json_dumps_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s">'indent'</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">object</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_object</span><span class="p">()</span>
<span class="n">context_dict</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">'name'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s">'location'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">location</span>
<span class="p">}</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_json_response</span><span class="p">(</span><span class="n">context_dict</span><span class="p">)</span>
</pre></div>
</div>
<p>You can additionally use the <cite>AjaxResponseMixin</cite></p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># views.py</span>
<span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">AjaxResponseMixin</span>
<span class="k">class</span> <span class="nc">UserProfileView</span><span class="p">(</span><span class="n">JSONResponseMixin</span><span class="p">,</span> <span class="n">AjaxResponseMixin</span><span class="p">,</span> <span class="n">DetailView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Profile</span>
<span class="k">def</span> <span class="nf">get_ajax</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_json_object_response</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_object</span><span class="p">())</span>
</pre></div>
</div>
<p>The <cite>JSONResponseMixin</cite> provides a class-level variable to control the response
type as well. By default it is <cite>application/json</cite>, but you can override that by
providing the <cite>content_type</cite> variable a different value or, programmatically, by
overriding the <cite>get_content_type()</cite> method.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">JSONResponseMixin</span>
<span class="k">class</span> <span class="nc">UserProfileAJAXView</span><span class="p">(</span><span class="n">JSONResponseMixin</span><span class="p">,</span> <span class="n">DetailView</span><span class="p">):</span>
<span class="n">content_type</span> <span class="o">=</span> <span class="s">'application/javascript'</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Profile</span>
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">object</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_object</span><span class="p">()</span>
<span class="n">context_dict</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">'name'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s">'location'</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">object</span><span class="o">.</span><span class="n">location</span>
<span class="p">}</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_json_response</span><span class="p">(</span><span class="n">context_dict</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_content_type</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c"># Shown just for illustrative purposes</span>
<span class="k">return</span> <span class="s">'application/javascript'</span>
</pre></div>
</div>
</div>
<div class="section" id="jsonrequestresponsemixin">
<span id="id5"></span><h2><a class="toc-backref" href="#id16">JsonRequestResponseMixin</a><a class="headerlink" href="#jsonrequestresponsemixin" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.3.</span></p>
</div>
<p>A mixin that attempts to parse request as JSON. If request is properly formatted, the json is saved to self.request_json as a Python object. request_json will be None for imparsible requests.</p>
<p>To catch requests that aren’t JSON-formatted, set the class attribute <tt class="docutils literal"><span class="pre">require_json</span></tt> to True.</p>
<p>Override the class attribute <tt class="docutils literal"><span class="pre">error_response_dict</span></tt> to customize the default error message.</p>
<p>It extends <a class="reference internal" href="#jsonresponsemixin"><em>JSONResponseMixin</em></a>, so those utilities are available as well.</p>
<p>Note: To allow public access to your view, you’ll need to use the <tt class="docutils literal"><span class="pre">csrf_exempt</span></tt> decorator or <a class="reference internal" href="form.html#csrfexemptmixin"><em>CsrfExemptMixin</em></a>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">View</span>
<span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">CsrfExemptMixin</span><span class="p">,</span> <span class="n">JsonRequestResponseMixin</span>
<span class="k">class</span> <span class="nc">SomeView</span><span class="p">(</span><span class="n">CsrfExemptMixin</span><span class="p">,</span> <span class="n">JsonRequestResponseMixin</span><span class="p">,</span> <span class="n">View</span><span class="p">):</span>
<span class="n">require_json</span> <span class="o">=</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">burrito</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request_json</span><span class="p">[</span><span class="s">'burrito'</span><span class="p">]</span>
<span class="n">toppings</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request_json</span><span class="p">[</span><span class="s">'toppings'</span><span class="p">]</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">error_dict</span> <span class="o">=</span> <span class="p">{</span><span class="s">'message'</span><span class="p">:</span>
<span class="s">'your order must include a burrito AND toppings'</span><span class="p">}</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_bad_request_response</span><span class="p">(</span><span class="n">error_dict</span><span class="p">)</span>
<span class="n">place_order</span><span class="p">(</span><span class="n">burrito</span><span class="p">,</span> <span class="n">toppings</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_json_response</span><span class="p">(</span>
<span class="p">{</span><span class="s">'message'</span><span class="p">:</span> <span class="s">'Your order has been placed!'</span><span class="p">})</span>
</pre></div>
</div>
</div>
<div class="section" id="ajaxresponsemixin">
<span id="id6"></span><h2><a class="toc-backref" href="#id17">AjaxResponseMixin</a><a class="headerlink" href="#ajaxresponsemixin" title="Permalink to this headline">¶</a></h2>
<p>A mixin to allow you to provide alternative methods for handling AJAX requests.</p>
<p>To control AJAX-specific behavior, override <tt class="docutils literal"><span class="pre">get_ajax</span></tt>, <tt class="docutils literal"><span class="pre">post_ajax</span></tt>, <tt class="docutils literal"><span class="pre">put_ajax</span></tt>, or <tt class="docutils literal"><span class="pre">delete_ajax</span></tt>. All four methods take <tt class="docutils literal"><span class="pre">request</span></tt>, <tt class="docutils literal"><span class="pre">*args</span></tt>, and <tt class="docutils literal"><span class="pre">**kwargs</span></tt> like the standard view methods.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># views.py</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">View</span>
<span class="kn">from</span> <span class="nn">braces.views</span> <span class="kn">import</span> <span class="n">AjaxResponseMixin</span><span class="p">,</span> <span class="n">JSONResponseMixin</span>
<span class="k">class</span> <span class="nc">SomeView</span><span class="p">(</span><span class="n">JSONResponseMixin</span><span class="p">,</span> <span class="n">AjaxResponseMixin</span><span class="p">,</span> <span class="n">View</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">get_ajax</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">json_dict</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">'name'</span><span class="p">:</span> <span class="s">"Benny's Burritos"</span><span class="p">,</span>
<span class="s">'location'</span><span class="p">:</span> <span class="s">"New York, NY"</span>
<span class="p">}</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_json_response</span><span class="p">(</span><span class="n">json_dict</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="orderablelistmixin">
<span id="id7"></span><h2><a class="toc-backref" href="#id18">OrderableListMixin</a><a class="headerlink" href="#orderablelistmixin" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.1.</span></p>
</div>
<p>A mixin to allow easy ordering of your queryset basing on the GET parameters. Works with <cite>ListView</cite>.</p>
<p>To use it, define columns that the data can be order by as well as the default column to order by in your view. This can be done either by simply setting the class attributes...</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># views.py</span>
<span class="k">class</span> <span class="nc">OrderableListView</span><span class="p">(</span><span class="n">OrderableListMixin</span><span class="p">,</span> <span class="n">ListView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Article</span>
<span class="n">orderable_columns</span> <span class="o">=</span> <span class="p">(</span><span class="s">'id'</span><span class="p">,</span> <span class="s">'title'</span><span class="p">,)</span>
<span class="n">orderable_columns_default</span> <span class="o">=</span> <span class="s">'id'</span>
</pre></div>
</div>
<p>...or by using similarly name methods to set the ordering constraints more dynamically:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># views.py</span>
<span class="k">class</span> <span class="nc">OrderableListView</span><span class="p">(</span><span class="n">OrderableListMixin</span><span class="p">,</span> <span class="n">ListView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Article</span>
<span class="k">def</span> <span class="nf">get_orderable_columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c"># return an iterable</span>
<span class="k">return</span> <span class="p">(</span><span class="s">'id'</span><span class="p">,</span> <span class="s">'title'</span><span class="p">,</span> <span class="p">)</span>
<span class="k">def</span> <span class="nf">get_orderable_columns_default</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c"># return a string</span>
<span class="k">return</span> <span class="s">'id'</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">orderable_columns</span></tt> restriction is here in order to stop your users from launching inefficient queries, like ordering by binary columns.</p>
<p><tt class="docutils literal"><span class="pre">OrderableListMixin</span></tt> will order your queryset basing on following GET params:</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">order_by</span></tt>: column name, e.g. <tt class="docutils literal"><span class="pre">'title'</span></tt></li>
<li><tt class="docutils literal"><span class="pre">ordering</span></tt>: <cite>‘asc’</cite> (default) or <tt class="docutils literal"><span class="pre">'desc'</span></tt></li>
</ul>
</div></blockquote>
<p>Example url: <cite>http://127.0.0.1:8000/articles/?order_by=title&ordering=asc</cite></p>
</div>
<div class="section" id="canonicalslugdetailmixin">
<span id="id8"></span><h2><a class="toc-backref" href="#id19">CanonicalSlugDetailMixin</a><a class="headerlink" href="#canonicalslugdetailmixin" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.3.</span></p>
</div>
<p>A mixin that enforces a canonical slug in the url. Works with <tt class="docutils literal"><span class="pre">DetailView</span></tt>.</p>
<p>If a urlpattern takes a object’s pk and slug as arguments and the slug url argument does not equal the object’s canonical slug, this mixin will redirect to the url containing the canonical slug.</p>
<p>To use it, the urlpattern must accept both a <tt class="docutils literal"><span class="pre">pk</span></tt> and <tt class="docutils literal"><span class="pre">slug</span></tt> argument in its regex:</p>
<div class="highlight-python"><div class="highlight"><pre># urls.py
urlpatterns = patterns('',
url(r'^article/(?P<pk>\d+)-(?P<slug>[-\w]+)$')
ArticleView.as_view(),
'view_article'
)
</pre></div>
</div>
<p>Then create a standard DetailView that inherits this mixin:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ArticleView</span><span class="p">(</span><span class="n">CanonicalSlugDetailMixin</span><span class="p">,</span> <span class="n">DetailView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Article</span>
</pre></div>
</div>
<p>Now, given an Article object with <tt class="docutils literal"><span class="pre">{pk:</span> <span class="pre">1,</span> <span class="pre">slug:</span> <span class="pre">'hello-world'}</span></tt>, the url <cite>http://127.0.0.1:8000/article/1-goodbye-moon</cite> will redirect to <cite>http://127.0.0.1:8000/article/1-hello-world</cite> with the HTTP status code 301 Moved Permanently. Any other non-canonical slug, not just ‘goodbye-moon’, will trigger the redirect as well.</p>
<p>Control the canonical slug by either implementing the method <tt class="docutils literal"><span class="pre">get_canonical_slug()</span></tt> on the model class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
<span class="n">blog</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="s">'Blog'</span><span class="p">)</span>
<span class="n">slug</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">SlugField</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">get_canonical_slug</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"{}-{}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">blog</span><span class="o">.</span><span class="n">get_canonical_slug</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">slug</span><span class="p">)</span>
</pre></div>
</div>
<p>Or by overriding the <tt class="docutils literal"><span class="pre">get_canonical_slug()</span></tt> method on the view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ArticleView</span><span class="p">(</span><span class="n">CanonicalSlugDetailMixin</span><span class="p">,</span> <span class="n">DetailView</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Article</span>
<span class="k">def</span> <span class="nf">get_canonical_slug</span><span class="p">():</span>
<span class="kn">import</span> <span class="nn">codecs</span>
<span class="k">return</span> <span class="n">codecs</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_object</span><span class="p">()</span><span class="o">.</span><span class="n">slug</span><span class="p">,</span> <span class="s">'rot_13'</span><span class="p">)</span>
</pre></div>
</div>
<p>Given the same Article as before, this will generate urls of <cite>http://127.0.0.1:8000/article/1-my-blog-hello-world</cite> and <cite>http://127.0.0.1:8000/article/1-uryyb-jbeyq</cite>, respectively.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Other Mixins</a><ul>
<li><a class="reference internal" href="#setheadlinemixin">SetHeadlineMixin</a><ul>
<li><a class="reference internal" href="#static-example">Static Example</a></li>
<li><a class="reference internal" href="#dynamic-example">Dynamic Example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#selectrelatedmixin">SelectRelatedMixin</a></li>
<li><a class="reference internal" href="#prefetchrelatedmixin">PrefetchRelatedMixin</a></li>
<li><a class="reference internal" href="#jsonresponsemixin">JSONResponseMixin</a></li>
<li><a class="reference internal" href="#jsonrequestresponsemixin">JsonRequestResponseMixin</a></li>
<li><a class="reference internal" href="#ajaxresponsemixin">AjaxResponseMixin</a></li>
<li><a class="reference internal" href="#orderablelistmixin">OrderableListMixin</a></li>
<li><a class="reference internal" href="#canonicalslugdetailmixin">CanonicalSlugDetailMixin</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="form.html"
title="previous chapter">Form Mixins</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/other.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="form.html" title="Form Mixins"
>previous</a> |</li>
<li><a href="index.html">django-braces 1.3.1 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2013, Kenneth Love and Chris Jones.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.1.
</div>
</body>
</html>
|