{"title":"Coding style","version":"2.1","locale":"fr","docname":"internals/contributing/writing-code/coding-style","url":"/fr/2.1/internals/contributing/writing-code/coding-style/","canonical":"https://djangodocs.dev/fr/2.1/internals/contributing/writing-code/coding-style/","summary":"Please follow these coding standards when writing code for inclusion in Django. Please conform to the indentation style dictated in the.editorconfig file.","html":"<h1>Coding style<a class=\"heading-anchor\" href=\"#coding-style\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Please follow these coding standards when writing code for inclusion in Django.</p>\n<section id=\"python-style\">\n<span id=\"coding-style-python\"></span><h2>Python style<a class=\"heading-anchor\" href=\"#python-style\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul>\n<li><p>Please conform to the indentation style dictated in the <code class=\"docutils literal notranslate\">.editorconfig</code>\nfile. We recommend using a text editor with <a class=\"reference external\" href=\"https://editorconfig.org/\">EditorConfig</a> support to avoid\nindentation and whitespace issues. The Python files use 4 spaces for\nindentation and the HTML files use 2 spaces.</p></li>\n<li><p>Unless otherwise specified, follow <span class=\"target\" id=\"index-0\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0008/\"><strong>PEP 8</strong></a>.</p>\n<p>Use <a class=\"reference external\" href=\"https://pypi.org/project/flake8/\">flake8</a> to check for problems in this area. Note that our <code class=\"docutils literal notranslate\">setup.cfg</code>\nfile contains some excluded files (deprecated modules we don’t care about\ncleaning up and some third-party code that Django vendors) as well as some\nexcluded errors that we don’t consider as gross violations. Remember that\n<span class=\"target\" id=\"index-1\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0008/\"><strong>PEP 8</strong></a> is only a guide, so respect the style of the surrounding code as a\nprimary goal.</p>\n<p>An exception to <span class=\"target\" id=\"index-2\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0008/\"><strong>PEP 8</strong></a> is our rules on line lengths. Don’t limit lines of\ncode to 79 characters if it means the code looks significantly uglier or is\nharder to read. We allow up to 119 characters as this is the width of GitHub\ncode review; anything longer requires horizontal scrolling which makes review\nmore difficult. This check is included when you run <code class=\"docutils literal notranslate\">flake8</code>. Documentation,\ncomments, and docstrings should be wrapped at 79 characters, even though\n<span class=\"target\" id=\"index-3\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0008/\"><strong>PEP 8</strong></a> suggests 72.</p>\n</li>\n<li><p>Utilisez 4 espaces pour l’indentation.</p></li>\n<li><p>Use four space hanging indentation rather than vertical alignment:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">raise</span> <span class=\"ne\">AttributeError</span><span class=\"p\">(</span>\n    <span class=\"s1\">&#39;Here is a multiline error message &#39;</span>\n    <span class=\"s1\">&#39;shortened for clarity.&#39;</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>Instead of:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">raise</span> <span class=\"ne\">AttributeError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Here is a multiline error message &#39;</span>\n                     <span class=\"s1\">&#39;shortened for clarity.&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This makes better use of space and avoids having to realign strings if the\nlength of the first line changes.</p>\n</li>\n<li><p>Use single quotes for strings, or a double quote if the string contains a\nsingle quote. Don’t waste time doing unrelated refactoring of existing code\nto conform to this style.</p></li>\n<li><p>Avoid use of « we » in comments, e.g. « Loop over » rather than « We loop over ».</p></li>\n<li><p>Use underscores, not camelCase, for variable, function and method names\n(i.e. <code class=\"docutils literal notranslate\">poll.get_unique_voters()</code>, not <code class=\"docutils literal notranslate\">poll.getUniqueVoters()</code>).</p></li>\n<li><p>Use <code class=\"docutils literal notranslate\">InitialCaps</code> for class names (or for factory functions that\nreturn classes).</p></li>\n<li><p>In docstrings, follow the style of existing docstrings and <span class=\"target\" id=\"index-4\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-0257/\"><strong>PEP 257</strong></a>.</p></li>\n<li><p>In tests, use\n<a class=\"reference internal\" href=\"/fr/2.1/topics/testing/tools/#django.test.SimpleTestCase.assertRaisesMessage\" title=\"django.test.SimpleTestCase.assertRaisesMessage\"><code class=\"xref py py-meth docutils literal notranslate\">assertRaisesMessage()</code></a> and\n<a class=\"reference internal\" href=\"/fr/2.1/topics/testing/tools/#django.test.SimpleTestCase.assertWarnsMessage\" title=\"django.test.SimpleTestCase.assertWarnsMessage\"><code class=\"xref py py-meth docutils literal notranslate\">assertWarnsMessage()</code></a>\ninstead of <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises\" title=\"(disponible dans Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertRaises()</code></a> and\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertWarns\" title=\"(disponible dans Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertWarns()</code></a> so you can check the\nexception or warning message. Use <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaisesRegex\" title=\"(disponible dans Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertRaisesRegex()</code></a>\nand <a class=\"reference external\" href=\"https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertWarnsRegex\" title=\"(disponible dans Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">assertWarnsRegex()</code></a> only if you need regular\nexpression matching.</p></li>\n<li><p>In test docstrings, state the expected behavior that each test demonstrates.\nDon’t include preambles such as « Tests that » or « Ensures that ».</p>\n<p>Reserve ticket references for obscure issues where the ticket has additional\ndetails that can’t be easily described in docstrings or comments. Include the\nticket number at the end of a sentence like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">test_foo</span><span class=\"p\">():</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    A test docstring looks like this (#123456).</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"imports\">\n<span id=\"coding-style-imports\"></span><h2>Importations<a class=\"heading-anchor\" href=\"#imports\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul>\n<li><p>Use <a class=\"reference external\" href=\"https://github.com/timothycrosley/isort#readme\">isort</a> to automate\nimport sorting using the guidelines below.</p>\n<p>Quick start:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>pip install isort\n<span class=\"gp\">$ </span>isort -rc .\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> pip install isort\n<span class=\"gp\">...\\&gt;</span> isort -rc .\n</code></pre></div></div></div>\n<p>This runs <code class=\"docutils literal notranslate\">isort</code> recursively from your current directory, modifying any\nfiles that don’t conform to the guidelines. If you need to have imports out\nof order (to avoid a circular import, for example) use a comment like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span> <span class=\"nn\">module</span>  <span class=\"c1\"># isort:skip</span>\n</code></pre></div>\n</li>\n<li><p>Put imports in these groups: future, standard library, third-party libraries,\nother Django components, local Django component, try/excepts. Sort lines in\neach group alphabetically by the full module name. Place all <code class=\"docutils literal notranslate\">import module</code>\nstatements before <code class=\"docutils literal notranslate\">from module import objects</code> in each section. Use absolute\nimports for other Django components and relative imports for local components.</p></li>\n<li><p>Sur chaque ligne, triez les éléments par ordre alphabétique en groupant les noms avec majuscule avant les noms avec minuscule.</p></li>\n<li><p>Séparez les longues lignes par des parenthèses et indentez les lignes de continuation par 4 espaces. Ajoutez une virgule finale après la dernière importation et placez la parenthèse fermante sur sa propre ligne.</p>\n<p>Placez une seule ligne vierge entre la dernière importation et tout code de niveau module, et placez deux lignes vierges au-dessus de la première fonction ou classe.</p>\n<p>Par exemple (les commentaires ne sont qu’à titre informatif) :</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">django/contrib/admin/example.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"c1\"># future</span>\n<span class=\"kn\">from</span> <span class=\"nn\">__future__</span> <span class=\"kn\">import</span> unicode_literals\n\n<span class=\"c1\"># standard library</span>\n<span class=\"kn\">import</span> <span class=\"nn\">json</span>\n<span class=\"kn\">from</span> <span class=\"nn\">itertools</span> <span class=\"kn\">import</span> chain\n\n<span class=\"c1\"># third-party</span>\n<span class=\"kn\">import</span> <span class=\"nn\">bcrypt</span>\n\n<span class=\"c1\"># Django</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> Http404\n<span class=\"kn\">from</span> <span class=\"nn\">django.http.response</span> <span class=\"kn\">import</span> <span class=\"p\">(</span>\n    Http404<span class=\"p\">,</span> HttpResponse<span class=\"p\">,</span> HttpResponseNotAllowed<span class=\"p\">,</span> StreamingHttpResponse<span class=\"p\">,</span>\n    cookie<span class=\"p\">,</span>\n<span class=\"p\">)</span>\n\n<span class=\"c1\"># local Django</span>\n<span class=\"kn\">from</span> <span class=\"nn\">.models</span> <span class=\"kn\">import</span> LogEntry\n\n<span class=\"c1\"># try/except</span>\n<span class=\"k\">try</span><span class=\"p\">:</span>\n    <span class=\"kn\">import</span> <span class=\"nn\">yaml</span>\n<span class=\"k\">except</span> <span class=\"ne\">ImportError</span><span class=\"p\">:</span>\n    yaml <span class=\"o\">=</span> <span class=\"kc\">None</span>\n\nCONSTANT <span class=\"o\">=</span> <span class=\"s1\">&#39;foo&#39;</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">Example</span><span class=\"p\">:</span>\n    <span class=\"c1\"># ...</span>\n</code></pre></figure>\n</li>\n<li><p>Utilisez des importations les plus directes possibles quand elles sont disponibles</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views</span> <span class=\"kn\">import</span> View\n</code></pre></div>\n<p>au lieu de</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.base</span> <span class=\"kn\">import</span> View\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"template-style\">\n<h2>Style des gabarits<a class=\"heading-anchor\" href=\"#template-style\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul>\n<li><p>Dans le code des gabarits Django, placez un et un seul espace entre les accolades et le contenu des balises.</p>\n<p>Faites :</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span> <span class=\"nv\">foo</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n<p>Ne faites pas :</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{{</span><span class=\"nv\">foo</span><span class=\"cp\">}}</span>\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"view-style\">\n<h2>Style des vues<a class=\"heading-anchor\" href=\"#view-style\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul>\n<li><p>Dans les vues de Django, le premier paramètre d’une vue fonction devrait s’appeler <code class=\"docutils literal notranslate\">request</code>.</p>\n<p>Faites</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">,</span> foo<span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n<p>Ne faites pas :</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>req<span class=\"p\">,</span> foo<span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"model-style\">\n<h2>Style des modèles<a class=\"heading-anchor\" href=\"#model-style\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul>\n<li><p>Les noms de champs devraient être tout en minuscules, en utilisant des soulignements au lieu du StyleChameau.</p>\n<p>Faites</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    first_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">20</span><span class=\"p\">)</span>\n    last_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Ne faites pas :</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    FirstName <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">20</span><span class=\"p\">)</span>\n    Last_Name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n<li><p>La <code class=\"docutils literal notranslate\">class Meta</code> doit apparaître <em>après</em> les définitions de champs, avec une seule ligne vierge séparant les champs de la définition de classe.</p>\n<p>Faites</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    first_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">20</span><span class=\"p\">)</span>\n    last_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">)</span>\n\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        verbose_name_plural <span class=\"o\">=</span> <span class=\"s1\">&#39;people&#39;</span>\n</code></pre></div>\n<p>Ne faites pas :</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    first_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">20</span><span class=\"p\">)</span>\n    last_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">)</span>\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        verbose_name_plural <span class=\"o\">=</span> <span class=\"s1\">&#39;people&#39;</span>\n</code></pre></div>\n<p>Ne faites pas ça non plus :</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        verbose_name_plural <span class=\"o\">=</span> <span class=\"s1\">&#39;people&#39;</span>\n\n    first_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">20</span><span class=\"p\">)</span>\n    last_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">)</span>\n</code></pre></div>\n</li>\n<li><p>L’ordre des classes internes de modèles et des méthodes standards doit être le suivant (notez qu’elles ne sont pas toutes obligatoires) :</p>\n<ul class=\"simple\">\n<li><p>Tous les champs de la base de données</p></li>\n<li><p>Attributs des gestionnaires personnalisés</p></li>\n<li><p><code class=\"docutils literal notranslate\">class Meta</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">def __str__()</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">def save()</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">def get_absolute_url()</code></p></li>\n<li><p>Toute autre méthode personnalisée</p></li>\n</ul>\n</li>\n<li><p>Si <code class=\"docutils literal notranslate\">choices</code> est défini pour un champ de modèle, définissez chaque choix sous forme de tuple de tuples, avec un nom tout en majuscules comme attribut de classe du modèle. Exemple</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">MyModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    DIRECTION_UP <span class=\"o\">=</span> <span class=\"s1\">&#39;U&#39;</span>\n    DIRECTION_DOWN <span class=\"o\">=</span> <span class=\"s1\">&#39;D&#39;</span>\n    DIRECTION_CHOICES <span class=\"o\">=</span> <span class=\"p\">(</span>\n        <span class=\"p\">(</span>DIRECTION_UP<span class=\"p\">,</span> <span class=\"s1\">&#39;Up&#39;</span><span class=\"p\">),</span>\n        <span class=\"p\">(</span>DIRECTION_DOWN<span class=\"p\">,</span> <span class=\"s1\">&#39;Down&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"use-of-django-conf-settings\">\n<h2>Utilisation de <code class=\"docutils literal notranslate\">django.conf.settings</code><a class=\"heading-anchor\" href=\"#use-of-django-conf-settings\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Les modules ne devraient généralement pas utiliser des réglages stockés dans <code class=\"docutils literal notranslate\">django.conf.settings</code> à leur premier niveau (c’est-à-dire évalués au moment de l’importation du module). Voici pourquoi :</p>\n<p>La configuration manuelle des réglages (qui ne dépend pas de la variable d’environnement <code class=\"docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code>) est autorisée et possible comme ceci</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n\nsettings<span class=\"o\">.</span>configure<span class=\"p\">({},</span> SOME_SETTING<span class=\"o\">=</span><span class=\"s1\">&#39;foo&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>However, if any setting is accessed before the <code class=\"docutils literal notranslate\">settings.configure</code> line,\nthis will not work. (Internally, <code class=\"docutils literal notranslate\">settings</code> is a <code class=\"docutils literal notranslate\">LazyObject</code> which\nconfigures itself automatically when the settings are accessed if it has not\nalready been configured).</p>\n<p>So, if there is a module containing some code as follows:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> get_callable\n\ndefault_foo_view <span class=\"o\">=</span> get_callable<span class=\"p\">(</span>settings<span class=\"o\">.</span>FOO_VIEW<span class=\"p\">)</span>\n</code></pre></div>\n<p>…then importing this module will cause the settings object to be configured.\nThat means that the ability for third parties to import the module at the top\nlevel is incompatible with the ability to configure the settings object\nmanually, or makes it very difficult in some circumstances.</p>\n<p>Instead of the above code, a level of laziness or indirection must be used,\nsuch as <code class=\"docutils literal notranslate\">django.utils.functional.LazyObject</code>,\n<code class=\"docutils literal notranslate\">django.utils.functional.lazy()</code> or <code class=\"docutils literal notranslate\">lambda</code>.</p>\n</section>\n<section id=\"miscellaneous\">\n<h2>Divers<a class=\"heading-anchor\" href=\"#miscellaneous\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul class=\"simple\">\n<li><p>Marquez toutes les chaînes à traduire ; voir la  <a class=\"reference internal\" href=\"/fr/2.1/topics/i18n/\"><span class=\"doc\">documentation i18n</span></a> pour plus de détails.</p></li>\n<li><p>Remove <code class=\"docutils literal notranslate\">import</code> statements that are no longer used when you change code.\n<a class=\"reference external\" href=\"https://pypi.org/project/flake8/\">flake8</a> will identify these imports for you. If an unused import needs to\nremain for backwards-compatibility, mark the end of with <code class=\"docutils literal notranslate\"># NOQA</code> to\nsilence the flake8 warning.</p></li>\n<li><p>Systematically remove all trailing whitespaces from your code as those\nadd unnecessary bytes, add visual clutter to the patches and can also\noccasionally cause unnecessary merge conflicts. Some IDE’s can be\nconfigured to automatically remove them and most VCS tools can be set to\nhighlight them in diff outputs.</p></li>\n<li><p>Please don’t put your name in the code you contribute. Our policy is to\nkeep contributors” names in the <code class=\"docutils literal notranslate\">AUTHORS</code> file distributed with Django\n– not scattered throughout the codebase itself. Feel free to include a\nchange to the <code class=\"docutils literal notranslate\">AUTHORS</code> file in your patch if you make more than a\nsingle trivial change.</p></li>\n</ul>\n</section>\n<section id=\"javascript-style\">\n<h2>Style JavaScript<a class=\"heading-anchor\" href=\"#javascript-style\"><span class=\"visually-hidden\">Lien vers cette rubrique</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Pour plus de détails sur le style de code JavaScript utilisé par Django, consultez <a class=\"reference internal\" href=\"/fr/2.1/internals/contributing/writing-code/javascript/\"><span class=\"doc\">JavaScript</span></a>.</p>\n</section>","rootId":"coding-style","toc":[{"title":"Python style","anchor":"python-style","children":[]},{"title":"Importations","anchor":"imports","children":[]},{"title":"Style des gabarits","anchor":"template-style","children":[]},{"title":"Style des vues","anchor":"view-style","children":[]},{"title":"Style des modèles","anchor":"model-style","children":[]},{"title":"Utilisation de django.conf.settings","anchor":"use-of-django-conf-settings","children":[]},{"title":"Divers","anchor":"miscellaneous","children":[]},{"title":"Style JavaScript","anchor":"javascript-style","children":[]}],"breadcrumbs":[{"docname":"internals/index","title":"Fonctionnement interne du projet Django","url":"/fr/2.1/internals/"},{"docname":"internals/contributing/index","title":"Contribuer à Django","url":"/fr/2.1/internals/contributing/"},{"docname":"internals/contributing/writing-code/index","title":"Écriture du code","url":"/fr/2.1/internals/contributing/writing-code/"}],"prev":{"docname":"internals/contributing/writing-code/index","title":"Écriture du code","url":"/fr/2.1/internals/contributing/writing-code/"},"next":{"docname":"internals/contributing/writing-code/unit-tests","title":"Les tests unitaires","url":"/fr/2.1/internals/contributing/writing-code/unit-tests/"},"formats":{"html":"/fr/2.1/internals/contributing/writing-code/coding-style/","markdown":"/fr/2.1/internals/contributing/writing-code/coding-style.md","json":"/fr/2.1/internals/contributing/writing-code/coding-style.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/internals/contributing/writing-code/coding-style.txt","official":"https://docs.djangoproject.com/fr/2.1/internals/contributing/writing-code/coding-style/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}