Sophie

Sophie

distrib > Mageia > 6 > i586 > by-pkgid > 65530c6176058f9b54858c3b4f6385e6 > files > 645

python-django-doc-1.8.19-1.mga6.noarch.rpm

<!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" lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Coding style &#8212; Django 1.8.19 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.8.19',
        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="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="top" title="Django 1.8.19 documentation" href="../../../contents.html" />
    <link rel="up" title="Writing code" href="index.html" />
    <link rel="next" title="Unit tests" href="unit-tests.html" />
    <link rel="prev" title="Writing code" href="index.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body role="document">

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.8.19 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Writing code">previous</a>
     |
    <a href="../../index.html" title="Django internals" accesskey="U">up</a>
   |
    <a href="unit-tests.html" title="Unit tests">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="internals-contributing-writing-code-coding-style">
            
  <div class="section" id="s-coding-style">
<span id="coding-style"></span><h1>Coding style<a class="headerlink" href="#coding-style" title="Permalink to this headline">¶</a></h1>
<p>Please follow these coding standards when writing code for inclusion in Django.</p>
<div class="section" id="s-python-style">
<span id="python-style"></span><h2>Python style<a class="headerlink" href="#python-style" title="Permalink to this headline">¶</a></h2>
<ul>
<li><p class="first">Unless otherwise specified, follow <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a>.</p>
<p>Use <a class="reference external" href="https://pypi.python.org/pypi/flake8">flake8</a> to check for problems in this area. Note that our <code class="docutils literal"><span class="pre">setup.cfg</span></code>
file contains some excluded files (deprecated modules we don&#8217;t care about
cleaning up and some third-party code that Django vendors) as well as some
excluded errors that we don&#8217;t consider as gross violations. Remember that
<span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a> is only a guide, so respect the style of the surrounding code as a
primary goal.</p>
<p>An exception to <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a> is our rules on line lengths. Don&#8217;t limit lines of
code to 79 characters if it means the code looks significantly uglier or is
harder to read. We allow up to 119 characters as this is the width of GitHub
code review; anything longer requires horizontal scrolling which makes review
more difficult. This check is included when you run <code class="docutils literal"><span class="pre">flake8</span></code>. Documentation,
comments, and docstrings should be wrapped at 79 characters, even though
<span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a> suggests 72.</p>
</li>
<li><p class="first">Use four spaces for indentation.</p>
</li>
<li><p class="first">Use underscores, not camelCase, for variable, function and method names
(i.e. <code class="docutils literal"><span class="pre">poll.get_unique_voters()</span></code>, not <code class="docutils literal"><span class="pre">poll.getUniqueVoters</span></code>).</p>
</li>
<li><p class="first">Use <code class="docutils literal"><span class="pre">InitialCaps</span></code> for class names (or for factory functions that
return classes).</p>
</li>
<li><p class="first">Use convenience imports whenever available. For example, do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="k">import</span> <span class="n">View</span>
</pre></div>
</div>
<p>Don&#8217;t do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.base</span> <span class="k">import</span> <span class="n">View</span>
</pre></div>
</div>
</li>
<li><p class="first">In docstrings, use &#8220;action words&#8221; such as:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculates something and returns the result.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>Here&#8217;s an example of what not to do:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculate something and return the result.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>
</pre></div>
</div>
</li>
</ul>
</div>
<div class="section" id="s-template-style">
<span id="template-style"></span><h2>Template style<a class="headerlink" href="#template-style" title="Permalink to this headline">¶</a></h2>
<ul>
<li><p class="first">In Django template code, put one (and only one) space between the curly
brackets and the tag contents.</p>
<p>Do this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">foo</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Don&#8217;t do this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span></span><span class="cp">{{</span><span class="nv">foo</span><span class="cp">}}</span>
</pre></div>
</div>
</li>
</ul>
</div>
<div class="section" id="s-view-style">
<span id="view-style"></span><h2>View style<a class="headerlink" href="#view-style" title="Permalink to this headline">¶</a></h2>
<ul>
<li><p class="first">In Django views, the first parameter in a view function should be called
<code class="docutils literal"><span class="pre">request</span></code>.</p>
<p>Do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">foo</span><span class="p">):</span>
    <span class="c1"># ...</span>
</pre></div>
</div>
<p>Don&#8217;t do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="n">foo</span><span class="p">):</span>
    <span class="c1"># ...</span>
</pre></div>
</div>
</li>
</ul>
</div>
<div class="section" id="s-model-style">
<span id="model-style"></span><h2>Model style<a class="headerlink" href="#model-style" title="Permalink to this headline">¶</a></h2>
<ul>
<li><p class="first">Field names should be all lowercase, using underscores instead of
camelCase.</p>
<p>Do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</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">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>
</pre></div>
</div>
<p>Don&#8217;t do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</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">FirstName</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
    <span class="n">Last_Name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">The <code class="docutils literal"><span class="pre">class</span> <span class="pre">Meta</span></code> should appear <em>after</em> the fields are defined, with
a single blank line separating the fields and the class definition.</p>
<p>Do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</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">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s1">&#39;people&#39;</span>
</pre></div>
</div>
<p>Don&#8217;t do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</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">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s1">&#39;people&#39;</span>
</pre></div>
</div>
<p>Don&#8217;t do this, either:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</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="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s1">&#39;people&#39;</span>

    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">If you define a <code class="docutils literal"><span class="pre">__str__</span></code> method (previously <code class="docutils literal"><span class="pre">__unicode__</span></code> before Python 3
was supported), decorate the model class with
<a class="reference internal" href="../../../ref/utils.html#django.utils.encoding.python_2_unicode_compatible" title="django.utils.encoding.python_2_unicode_compatible"><code class="xref py py-func docutils literal"><span class="pre">python_2_unicode_compatible()</span></code></a>.</p>
</li>
<li><p class="first">The order of model inner classes and standard methods should be as
follows (noting that these are not all required):</p>
<ul class="simple">
<li>All database fields</li>
<li>Custom manager attributes</li>
<li><code class="docutils literal"><span class="pre">class</span> <span class="pre">Meta</span></code></li>
<li><code class="docutils literal"><span class="pre">def</span> <span class="pre">__str__()</span></code></li>
<li><code class="docutils literal"><span class="pre">def</span> <span class="pre">save()</span></code></li>
<li><code class="docutils literal"><span class="pre">def</span> <span class="pre">get_absolute_url()</span></code></li>
<li>Any custom methods</li>
</ul>
</li>
<li><p class="first">If <code class="docutils literal"><span class="pre">choices</span></code> is defined for a given model field, define each choice as
a tuple of tuples, with an all-uppercase name as a class attribute on the
model. Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModel</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">DIRECTION_UP</span> <span class="o">=</span> <span class="s1">&#39;U&#39;</span>
    <span class="n">DIRECTION_DOWN</span> <span class="o">=</span> <span class="s1">&#39;D&#39;</span>
    <span class="n">DIRECTION_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="n">DIRECTION_UP</span><span class="p">,</span> <span class="s1">&#39;Up&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="n">DIRECTION_DOWN</span><span class="p">,</span> <span class="s1">&#39;Down&#39;</span><span class="p">),</span>
    <span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
</div>
<div class="section" id="s-use-of-django-conf-settings">
<span id="use-of-django-conf-settings"></span><h2>Use of <code class="docutils literal"><span class="pre">django.conf.settings</span></code><a class="headerlink" href="#use-of-django-conf-settings" title="Permalink to this headline">¶</a></h2>
<p>Modules should not in general use settings stored in <code class="docutils literal"><span class="pre">django.conf.settings</span></code>
at the top level (i.e. evaluated when the module is imported). The explanation
for this is as follows:</p>
<p>Manual configuration of settings (i.e. not relying on the
<code class="docutils literal"><span class="pre">DJANGO_SETTINGS_MODULE</span></code> environment variable) is allowed and possible as
follows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>

<span class="n">settings</span><span class="o">.</span><span class="n">configure</span><span class="p">({},</span> <span class="n">SOME_SETTING</span><span class="o">=</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>However, if any setting is accessed before the <code class="docutils literal"><span class="pre">settings.configure</span></code> line,
this will not work. (Internally, <code class="docutils literal"><span class="pre">settings</span></code> is a <code class="docutils literal"><span class="pre">LazyObject</span></code> which
configures itself automatically when the settings are accessed if it has not
already been configured).</p>
<p>So, if there is a module containing some code as follows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="k">import</span> <span class="n">get_callable</span>

<span class="n">default_foo_view</span> <span class="o">=</span> <span class="n">get_callable</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">FOO_VIEW</span><span class="p">)</span>
</pre></div>
</div>
<p>...then importing this module will cause the settings object to be configured.
That means that the ability for third parties to import the module at the top
level is incompatible with the ability to configure the settings object
manually, or makes it very difficult in some circumstances.</p>
<p>Instead of the above code, a level of laziness or indirection must be used,
such as <code class="docutils literal"><span class="pre">django.utils.functional.LazyObject</span></code>,
<code class="docutils literal"><span class="pre">django.utils.functional.lazy()</span></code> or <code class="docutils literal"><span class="pre">lambda</span></code>.</p>
</div>
<div class="section" id="s-miscellaneous">
<span id="miscellaneous"></span><h2>Miscellaneous<a class="headerlink" href="#miscellaneous" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>Mark all strings for internationalization; see the <a class="reference internal" href="../../../topics/i18n/index.html"><span class="doc">i18n
documentation</span></a> for details.</li>
<li>Remove <code class="docutils literal"><span class="pre">import</span></code> statements that are no longer used when you change code.
<a class="reference external" href="https://pypi.python.org/pypi/flake8">flake8</a> will identify these imports for you. If an unused import needs to
remain for backwards-compatibility, mark the end of with <code class="docutils literal"><span class="pre">#</span> <span class="pre">NOQA</span></code> to
silence the flake8 warning.</li>
<li>Systematically remove all trailing whitespaces from your code as those
add unnecessary bytes, add visual clutter to the patches and can also
occasionally cause unnecessary merge conflicts. Some IDE&#8217;s can be
configured to automatically remove them and most VCS tools can be set to
highlight them in diff outputs.</li>
<li>Please don&#8217;t put your name in the code you contribute. Our policy is to
keep contributors&#8217; names in the <code class="docutils literal"><span class="pre">AUTHORS</span></code> file distributed with Django
&#8211; not scattered throughout the codebase itself. Feel free to include a
change to the <code class="docutils literal"><span class="pre">AUTHORS</span></code> file in your patch if you make more than a
single trivial change.</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Coding style</a><ul>
<li><a class="reference internal" href="#python-style">Python style</a></li>
<li><a class="reference internal" href="#template-style">Template style</a></li>
<li><a class="reference internal" href="#view-style">View style</a></li>
<li><a class="reference internal" href="#model-style">Model style</a></li>
<li><a class="reference internal" href="#use-of-django-conf-settings">Use of <code class="docutils literal"><span class="pre">django.conf.settings</span></code></a></li>
<li><a class="reference internal" href="#miscellaneous">Miscellaneous</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Writing code</a></li>
    
    
      <li>Next: <a href="unit-tests.html">Unit tests</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.8.19 documentation</a>
        
          <ul><li><a href="../../index.html">Django internals</a>
        
          <ul><li><a href="../index.html">Contributing to Django</a>
        
          <ul><li><a href="index.html">Writing code</a>
        
        <ul><li>Coding style</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/internals/contributing/writing-code/coding-style.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 10, 2018</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Writing code">previous</a>
     |
    <a href="../../index.html" title="Django internals" accesskey="U">up</a>
   |
    <a href="unit-tests.html" title="Unit tests">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>