{"title":"JavaScript","version":"2.0","locale":"zh-hans","docname":"internals/contributing/writing-code/javascript","url":"/zh-hans/2.0/internals/contributing/writing-code/javascript/","canonical":"https://djangodocs.dev/zh-hans/2.0/internals/contributing/writing-code/javascript/","summary":"While most of Django core is Python, the admin and gis contrib apps contain JavaScript code.","html":"<h1>JavaScript<a class=\"heading-anchor\" href=\"#javascript\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>While most of Django core is Python, the <code class=\"docutils literal notranslate\">admin</code> and <code class=\"docutils literal notranslate\">gis</code> contrib apps\ncontain JavaScript code.</p>\n<p>Please follow these coding standards when writing JavaScript code for inclusion\nin Django.</p>\n<section id=\"code-style\">\n<h2>Code style<a class=\"heading-anchor\" href=\"#code-style\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul class=\"simple\">\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=\"http://editorconfig.org/\">EditorConfig</a> support to avoid\nindentation and whitespace issues. Most of the JavaScript files use 4 spaces\nfor indentation, but there are some exceptions.</p></li>\n<li><p>When naming variables, use <code class=\"docutils literal notranslate\">camelCase</code> instead of <code class=\"docutils literal notranslate\">underscore_case</code>.\nDifferent JavaScript files sometimes use a different code style. Please try to\nconform to the code style of each file.</p></li>\n<li><p>Use the <a class=\"reference external\" href=\"http://jshint.com/\">JSHint</a> code linter to check your code for bugs and style errors.\nJSHint will be run when you run the JavaScript tests. We also recommended\ninstalling a JSHint plugin in your text editor.</p></li>\n<li><p>Where possible, write code that will work even if the page structure is later\nchanged with JavaScript. For instance, when binding a click handler, use\n<code class=\"docutils literal notranslate\">$('body').on('click', selector, func)</code> instead of\n<code class=\"docutils literal notranslate\">$(selector).click(func)</code>. This makes it easier for projects to extend\nDjango's default behavior with JavaScript.</p></li>\n</ul>\n</section>\n<section id=\"javascript-patches\">\n<span id=\"id1\"></span><h2>JavaScript patches<a class=\"heading-anchor\" href=\"#javascript-patches\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django's admin system leverages the jQuery framework to increase the\ncapabilities of the admin interface. In conjunction, there is an emphasis on\nadmin JavaScript performance and minimizing overall admin media file size.\nServing compressed or &quot;minified&quot; versions of JavaScript files is considered\nbest practice in this regard.</p>\n<p>To that end, patches for JavaScript files should include both the original\ncode for future development (e.g. <code class=\"docutils literal notranslate\">foo.js</code>), and a compressed version for\nproduction use (e.g. <code class=\"docutils literal notranslate\">foo.min.js</code>). Any links to the file in the codebase\nshould point to the compressed version.</p>\n<section id=\"compressing-javascript\">\n<h3>Compressing JavaScript<a class=\"heading-anchor\" href=\"#compressing-javascript\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>To simplify the process of providing optimized JavaScript code, Django\nincludes a handy Python script which should be used to create a &quot;minified&quot;\nversion. To run it:</p>\n<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 closure\n<span class=\"gp\">$ </span>python django/contrib/admin/bin/compress.py\n</code></pre></div>\n<p>Behind the scenes, <code class=\"docutils literal notranslate\">compress.py</code> is a front-end for Google's\n<a class=\"reference external\" href=\"https://developers.google.com/closure/compiler/\">Closure Compiler</a> which is written in Java. The Closure Compiler library is\nnot bundled with Django, but you can install it using pip as done above. The\nClosure Compiler library requires <a class=\"reference external\" href=\"https://www.java.com\">Java</a> 7 or higher.</p>\n<p>Please don't forget to run <code class=\"docutils literal notranslate\">compress.py</code> and include the <code class=\"docutils literal notranslate\">diff</code> of the\nminified scripts when submitting patches for Django's JavaScript.</p>\n</section>\n</section>\n<section id=\"javascript-tests\">\n<span id=\"id2\"></span><h2>JavaScript tests<a class=\"heading-anchor\" href=\"#javascript-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django's JavaScript tests can be run in a browser or from the command line.\nThe tests are located in a top level <code class=\"docutils literal notranslate\">js_tests</code> directory.</p>\n<section id=\"writing-tests\">\n<h3>Writing tests<a class=\"heading-anchor\" href=\"#writing-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django's JavaScript tests use <a class=\"reference external\" href=\"https://qunitjs.com/\">QUnit</a>. Here is an example test module:</p>\n<div class=\"code-block\" data-language=\"javascript\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">JavaScript</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=\"JavaScript code\"><code><span class=\"nx\">QUnit</span><span class=\"p\">.</span><span class=\"nx\">module</span><span class=\"p\">(</span><span class=\"s1\">&#39;magicTricks&#39;</span><span class=\"p\">,</span> <span class=\"p\">{</span>\n    <span class=\"nx\">beforeEach</span><span class=\"o\">:</span> <span class=\"kd\">function</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n        <span class=\"kd\">var</span> <span class=\"nx\">$</span> <span class=\"o\">=</span> <span class=\"nx\">django</span><span class=\"p\">.</span><span class=\"nx\">jQuery</span><span class=\"p\">;</span>\n        <span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s1\">&#39;#qunit-fixture&#39;</span><span class=\"p\">).</span><span class=\"nx\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;button class=&quot;button&quot;&gt;&lt;/button&gt;&#39;</span><span class=\"p\">);</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">});</span>\n\n<span class=\"nx\">QUnit</span><span class=\"p\">.</span><span class=\"nx\">test</span><span class=\"p\">(</span><span class=\"s1\">&#39;removeOnClick removes button on click&#39;</span><span class=\"p\">,</span> <span class=\"kd\">function</span><span class=\"p\">(</span><span class=\"nx\">assert</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"kd\">var</span> <span class=\"nx\">$</span> <span class=\"o\">=</span> <span class=\"nx\">django</span><span class=\"p\">.</span><span class=\"nx\">jQuery</span><span class=\"p\">;</span>\n    <span class=\"nx\">removeOnClick</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">);</span>\n    <span class=\"nx\">assert</span><span class=\"p\">.</span><span class=\"nx\">equal</span><span class=\"p\">(</span><span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">).</span><span class=\"nx\">length</span><span class=\"p\">,</span> <span class=\"mf\">1</span><span class=\"p\">);</span>\n    <span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">).</span><span class=\"nx\">click</span><span class=\"p\">();</span>\n    <span class=\"nx\">assert</span><span class=\"p\">.</span><span class=\"nx\">equal</span><span class=\"p\">(</span><span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">).</span><span class=\"nx\">length</span><span class=\"p\">,</span> <span class=\"mf\">0</span><span class=\"p\">);</span>\n<span class=\"p\">});</span>\n\n<span class=\"nx\">QUnit</span><span class=\"p\">.</span><span class=\"nx\">test</span><span class=\"p\">(</span><span class=\"s1\">&#39;copyOnClick adds button on click&#39;</span><span class=\"p\">,</span> <span class=\"kd\">function</span><span class=\"p\">(</span><span class=\"nx\">assert</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"kd\">var</span> <span class=\"nx\">$</span> <span class=\"o\">=</span> <span class=\"nx\">django</span><span class=\"p\">.</span><span class=\"nx\">jQuery</span><span class=\"p\">;</span>\n    <span class=\"nx\">copyOnClick</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">);</span>\n    <span class=\"nx\">assert</span><span class=\"p\">.</span><span class=\"nx\">equal</span><span class=\"p\">(</span><span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">).</span><span class=\"nx\">length</span><span class=\"p\">,</span> <span class=\"mf\">1</span><span class=\"p\">);</span>\n    <span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">).</span><span class=\"nx\">click</span><span class=\"p\">();</span>\n    <span class=\"nx\">assert</span><span class=\"p\">.</span><span class=\"nx\">equal</span><span class=\"p\">(</span><span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s1\">&#39;.button&#39;</span><span class=\"p\">).</span><span class=\"nx\">length</span><span class=\"p\">,</span> <span class=\"mf\">2</span><span class=\"p\">);</span>\n<span class=\"p\">});</span>\n</code></pre></div>\n<p>Please consult the QUnit documentation for information on the types of\n<a class=\"reference external\" href=\"https://api.qunitjs.com/assert/\">assertions supported by QUnit</a>.</p>\n</section>\n<section id=\"running-tests\">\n<h3>运行测试<a class=\"heading-anchor\" href=\"#running-tests\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The JavaScript tests may be run from a web browser or from the command line.</p>\n<section id=\"testing-from-a-web-browser\">\n<h4>Testing from a web browser<a class=\"heading-anchor\" href=\"#testing-from-a-web-browser\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To run the tests from a web browser, open up <code class=\"docutils literal notranslate\">js_tests/tests.html</code> in your\nbrowser.</p>\n<p>To measure code coverage when running the tests, you need to view that file\nover HTTP. To view code coverage:</p>\n<ul class=\"simple\">\n<li><p>Execute <code class=\"docutils literal notranslate\">python -m http.server</code> from the root directory (not from inside\n<code class=\"docutils literal notranslate\">js_tests</code>).</p></li>\n<li><p>Open <a class=\"reference external\" href=\"http://localhost:8000/js_tests/tests.html\">http://localhost:8000/js_tests/tests.html</a> in your web browser.</p></li>\n</ul>\n</section>\n<section id=\"testing-from-the-command-line\">\n<h4>Testing from the command line<a class=\"heading-anchor\" href=\"#testing-from-the-command-line\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To run the tests from the command line, you need to have <a class=\"reference external\" href=\"https://nodejs.org/\">Node.js</a> installed.</p>\n<p>After installing Node.js, install the JavaScript test dependencies by running\nthe following from the root of your Django checkout:</p>\n<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>npm install\n</code></pre></div>\n<p>Then run the tests with:</p>\n<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>npm <span class=\"nb\">test</span>\n</code></pre></div>\n</section>\n</section>\n</section>","rootId":"javascript","toc":[{"title":"Code style","anchor":"code-style","children":[]},{"title":"JavaScript patches","anchor":"javascript-patches","children":[{"title":"Compressing JavaScript","anchor":"compressing-javascript","children":[]}]},{"title":"JavaScript tests","anchor":"javascript-tests","children":[{"title":"Writing tests","anchor":"writing-tests","children":[]},{"title":"运行测试","anchor":"running-tests","children":[{"title":"Testing from a web browser","anchor":"testing-from-a-web-browser","children":[]},{"title":"Testing from the command line","anchor":"testing-from-the-command-line","children":[]}]}]}],"breadcrumbs":[{"docname":"internals/index","title":"Django internals","url":"/zh-hans/2.0/internals/"},{"docname":"internals/contributing/index","title":"建设 Django","url":"/zh-hans/2.0/internals/contributing/"},{"docname":"internals/contributing/writing-code/index","title":"Writing code","url":"/zh-hans/2.0/internals/contributing/writing-code/"}],"prev":{"docname":"internals/contributing/writing-code/working-with-git","title":"Working with Git and GitHub","url":"/zh-hans/2.0/internals/contributing/writing-code/working-with-git/"},"next":{"docname":"internals/contributing/writing-documentation","title":"编写文档","url":"/zh-hans/2.0/internals/contributing/writing-documentation/"},"formats":{"html":"/zh-hans/2.0/internals/contributing/writing-code/javascript/","markdown":"/zh-hans/2.0/internals/contributing/writing-code/javascript.md","json":"/zh-hans/2.0/internals/contributing/writing-code/javascript.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/internals/contributing/writing-code/javascript.txt","official":"https://docs.djangoproject.com/zh-hans/2.0/internals/contributing/writing-code/javascript/","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"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}