Management Commands

Django.js provide a management command to simplify some common JavaScript tasks:

$ python js -h
usage: js [-h] [-v {0,1,2,3}] [--settings SETTINGS]
                    [--pythonpath PYTHONPATH] [--traceback]
                    {bower,launcher,localize} ...

Handle javascript operations

optional arguments:
  -h, --help            show this help message and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
  --traceback           Print traceback on exception

  JavaScript command to execute

    bower               Generate a .bowerrc file
    launcher            Get a PhantomJS launcher path
    localize            Generate PO file from js files


The localize command generates a .po file for your javascript files. It allows you to localize your templates with custom patterns.

Custom patterns are specified in settings.JS_I18N_PATTERNS.

Let says you use Handlebars as client-side template engine, all your templates are .hbs files in your app static/templates directory and you registered a trans helper to handle localization:

Handlebars.registerHelper('trans', function(opt) {
    return gettext(opt.fn(this));

So, in your handlebars templates, you will have some localizable strings like:

{{#trans}}my translatable label{{/trans}}

You can add this to your file to extract localizable strings from them:

    ('hbs', 'static/templates', r'{{#trans}}(.*?){{/trans}}'),

Running the localize command:

$ python js localize -l fr

will extract all localizable strings from your .js files as usual and add those in your .hbs files.

$ python js localize -h
usage: js localize [-h] [--locale LOCALE] [--all]
                               [--extension EXTENSIONS] [--symlinks]
                               [--ignore PATTERN] [--no-default-ignore]
                               [--no-wrap] [--no-location] [--no-obsolete]
                               [app [app ...]]

Generate PO file from js files

positional arguments:
  app                   Applications to localize

optional arguments:
  -h, --help            show this help message and exit
  --locale LOCALE, -l LOCALE
                        Creates or updates the message files for the given
                        locale (e.g. pt_BR).
  --all, -a             Updates the message files for all existing locales.
                        The file extension(s) to examine (default: "js").
                        Separate multiple extensions with commas, or use -e
                        multiple times.
  --symlinks, -s        Follows symlinks to directories when examining source
                        code and templates for translation strings.
  --ignore PATTERN, -i PATTERN
                        Ignore files or directories matching this glob-style
                        pattern. Use multiple times to ignore more.
  --no-default-ignore   Don't ignore the common glob-style patterns 'CVS',
                        '.*' and '*~'.
  --no-wrap             Don't break long message lines into several lines
  --no-location         Don't write '#: filename:line' lines
  --no-obsolete         Remove obsolete message strings


The bower command generates a .bowerrc file into the current directory specifying the target directory for Bower downloads.

$ python js bower -h
usage: js bower [-h] [-f] target

Generate a .bowerrc file

positional arguments:
  target       The target directory for bower downloads

optional arguments:
  -h, --help   show this help message and exit
  -f, --force  Overwrite the file if exists


$ python js bower myproject/static/bower
Created .bowerrc file into the current directory
$ cat .bowerrc
{"directory": "./myproject/static/bower/"}


The launcher command returns the full path to a Django.js PhantomJS runner (usefull if you need to execute it manually).

$ python js launcher -h
usage: js launcher [-h] name

Get a PhantomJS launcher path

positional arguments:
  name        Runner name

optional arguments:
  -h, --help  show this help message and exit


$ python js launcher jasmine

Project Versions

Table Of Contents

Previous topic

Integration with other Django apps

Next topic


This Page