JavaScript

    Please follow these coding standards when writing JavaScript code for inclusion in Django.

    • When naming variables, use camelCase instead of . Different JavaScript files sometimes use a different code style. Please try to conform to the code style of each file.
    • Use the code linter to check your code for bugs and style errors. ESLint will be run when you run the JavaScript tests. We also recommended installing a ESLint plugin in your text editor.
    • Where possible, write code that will work even if the page structure is later changed with JavaScript. For instance, when binding a click handler, use $('body').on('click', selector, func) instead of $(selector).click(func). This makes it easier for projects to extend Django’s default behavior with JavaScript.

    Django’s admin system leverages the jQuery framework to increase the capabilities of the admin interface. In conjunction, there is an emphasis on admin JavaScript performance and minimizing overall admin media file size.

    Django’s JavaScript tests can be run in a browser or from the command line. The tests are located in a top level js_tests directory.

    Please consult the QUnit documentation for information on the types of assertions supported by QUnit.

    运行测试

    The JavaScript tests may be run from a web browser or from the command line.

    Testing from a web browser

    To run the tests from a web browser, open up in your browser.

    • Open in your web browser.

    Testing from the command line

    To run the tests from the command line, you need to have Node.js installed.

    After installing Node.js, install the JavaScript test dependencies by running the following from the root of your Django checkout:

    Linux/MacOS    Windows

    Linux/MacOS    Windows