djangojs – Main package

Django.js provide better integration of javascript into Django.

djangojs.JQUERY_DEFAULT_VERSION = '2.0.3'

Packaged jQuery version

djangojs.context_serializer – Context serialization handling

class djangojs.context_serializer.ContextSerializer(request)[source]

Bases: object

Serialize context and user from requests.

Inherits from this class and set your settings.JS_CONTEXT_PROCESSOR to customize the serialization.

To add a custom variable serialization handler, add a method named process_VARNAME(self, value, data).


Serialize the context as a dictionnary from a given request.


Serialize the context as JSON.


Insert user informations in data

Override it to add extra user attributes.

process_LANGUAGES(languages, data)[source]

Serialize LANGUAGES as a localized dictionnary.

process_LANGUAGE_CODE(language_code, data)[source]

Fix language code when set to non included dedfault en and add the extra variables LANGUAGE_NAME and LANGUAGE_NAME_LOCAL.

djangojs.views – Javascript views helpers

This module provide helper views for javascript.

class djangojs.views.JsInitView(**kwargs)[source]

Bases: djangojs.views.UserCacheMixin, django.views.generic.base.TemplateView

Render a javascript file containing the URLs mapping and the context as JSON.

class djangojs.views.JsonView(**kwargs)[source]

Bases: django.views.generic.base.View

A views that render JSON.

class djangojs.views.UrlsJsonView(**kwargs)[source]

Bases: djangojs.views.CacheMixin, djangojs.views.JsonView

Render the URLs as a JSON object.

class djangojs.views.ContextJsonView(**kwargs)[source]

Bases: djangojs.views.UserCacheMixin, djangojs.views.JsonView

Render the context as a JSON object.

class djangojs.views.JsTestView(**kwargs)[source]

Bases: django.views.generic.base.TemplateView

Base class for JS tests views

django_js = False

Includes or not Django.js in the test view

django_js_init = True

Initialize or not Django.js in the test view (only if included)

jquery = False

Includes or not jQuery in the test view.

js_files = None

A path or a list of path to javascript files to include into the view.

  • Supports glob patterns.
  • Order is kept for rendering.
class djangojs.views.JasmineView(**kwargs)[source]

Bases: djangojs.views.JsTestView

Render a Jasmine test runner.

class djangojs.views.QUnitView(**kwargs)[source]

Bases: djangojs.views.JsTestView

Render a QUnit test runner

theme = u'qunit'

QUnit runner theme.

Should be one of: qunit, gabe, ninja, nv

djangojs.runners – Javascript unittest runners

This module provide Javascript test runners for Django unittest.

class djangojs.runners.PhantomJsRunner[source]

Bases: object

Test helper to run JS tests with PhantomJS


Execute a subprocess yielding output lines

phantomjs(*args, **kwargs)[source]

Execute PhantomJS by giving args as command line arguments.

If test are run in verbose mode (-v/--verbosity = 2), it output:
  • the title as header (with separators before and after)
  • modules and test names
  • assertions results (with django.utils.termcolors support)

In case of error, a JsTestException is raised to give details about javascript errors.

phantomjs_runner = None

mandatory path to the PhantomJS javascript runner


Run a phantomjs test suite.

  • phantomjs_runner is mandatory.
  • Either url or url_name needs to be defined.
timeout = 3

PhantomJS execution timeout in seconds

title = u'PhantomJS test suite'

an optionnal title for verbose console output

url = None

an optionnal absolute URL to the test runner page

class djangojs.runners.JsTestCase(methodName='runTest')[source]

Bases: djangojs.runners.PhantomJsRunner, django.test.testcases.LiveServerTestCase

A PhantomJS suite that run against the Django LiveServerTestCase

url_args = None

an optionnal arguments array to pass to the reverse() function

url_kwargs = None

an optionnal keyword arguments dictionnary to pass to the reverse() function

url_name = None

a mandatory named URL that point to the test runner page

class djangojs.runners.JsFileTestCase(methodName='runTest')[source]

Bases: djangojs.runners.PhantomJsRunner, unittest.case.TestCase

A PhantomJS suite that run against a local html file

filename = None

absolute path to the test runner page

class djangojs.runners.JsTemplateTestCase(methodName='runTest')[source]

Bases: djangojs.runners.JsFileTestCase

A PhantomJS suite that run against a rendered html file but without server.


Template is rendered using a modified static storage that give file:// scheme URLs. To benefits from it, you have to use either the static template tag or one the djangojs template tags.


Template is not rendered within a request/response dialog. You can’t access the request object and everything that depends on the server.

jquery = False

Includes or not jQuery in the test view. Template has to handle the use_jquery property.

js_files = None

A path or a list of path to javascript files to include into the view.

  • Supports glob patterns.
  • Order is kept for rendering.
template_name = None

absolute path to the test runner page

exception djangojs.runners.JsTestException(message, failures=None)[source]

Bases: exceptions.Exception

An exception raised by Javascript tests.

It display javascript errors into the exception message.

class djangojs.runners.JasmineSuite[source]

Bases: object

A mixin that runs a jasmine test suite with PhantomJs.

class djangojs.runners.QUnitSuite[source]

Bases: object

A mixin that runs a QUnit test suite with PhantomJs.

class djangojs.runners.AbsoluteFileStorage(location=None, base_url=None)[source]

Bases: django.core.files.storage.FileSystemStorage

A storage that give the absolute file scheme URL as URL.

djangojs.urls_serializer – URLs serialization handling


Get the URLs mapping as a dictionnary


Get the URLs mapping as JSON

djangojs.utils – Miscellaneous helpers

This modules holds every helpers that does not fit in any standard django modules.


Get a python class object from its name

class djangojs.utils.LazyJsonEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None, use_decimal=True, namedtuple_as_object=True, tuple_as_array=True)[source]

Bases: django.core.serializers.json.DjangoJSONEncoder

A JSON encoder handling promises (aka. Django lazy objects).

See: https://docs.djangoproject.com/en/dev/topics/serialization/#id2

class djangojs.utils.StorageGlobber[source]

Bases: object

Retrieve file list from static file storages.

classmethod glob(files=None)[source]

Glob a pattern or a list of pattern static storage relative(s).

djangojs.tap – Tap format parser

This module provide test runners for JS in Django.

class djangojs.tap.TapParser(yield_class=<class 'djangojs.tap.TapTest'>, debug=False)[source]

Bases: object

A TAP parser class reading from iterable TAP lines.

djangojs.templatetags.js – Javascript template tags

Provide template tags to help with Javascript/Django integration.

class djangojs.templatetags.js.VerbatimNode(text_and_nodes)[source]

Bases: django.template.base.Node

Wrap {% verbatim %} and {% endverbatim %} around a block of javascript template and this will try its best to output the contents with no changes.

{% verbatim %}
    {% trans "Your name is" %} {{first}} {{last}}
{% endverbatim %}

A simple shortcut to render a script tag to a static coffeescript file


A simple shortcut to render a script tag to a static coffeescript file


A simple shortcut to render a link tag to a static CSS file

djangojs.templatetags.js.django_js(context, jquery=True, i18n=True, csrf=True, init=True)[source]

Include Django.js javascript library in the page

djangojs.templatetags.js.django_js_init(context, jquery=False, i18n=True, csrf=True, init=True)[source]

Include Django.js javascript library initialization in the page

djangojs.templatetags.js.javascript(filename, type=u'text/javascript')[source]

A simple shortcut to render a script tag to a static javascript file

djangojs.templatetags.js.jquery_js(version=None, migrate=False)[source]

A shortcut to render a script tag for the packaged jQuery

djangojs.templatetags.js.js(filename, type=u'text/javascript')[source]

A simple shortcut to render a script tag to a static javascript file

djangojs.templatetags.js.verbatim(parser, token)[source]

Renders verbatim tags

djangojs.templatetags.js.verbatim_tags(parser, token, endtagname)[source]

Javascript templates (jquery, handlebars.js, mustache.js) use constructs like:

{{if condition}} print something{{/if}}

This, of course, completely screws up Django templates, because Django thinks {{ and }} means something.

The following code preserves {{ }} tokens.

This version of verbatim template tag allows you to use tags like url {% url name %}. {% trans “foo” %} or {% csrf_token %} within.

Inspired by:

djangojs.context_processors – Context processors


Allow to use booleans in templates.

See: http://stackoverflow.com/questions/4557114/django-custom-template-tag-which-accepts-a-boolean-parameter

djangojs.contrib – Contributed compatibility modules

This package is dedicated to contributed compatibility modules.

These modules provide mixins and already packed ContextSerializers to use Django applications incompatible with the defaut Django.js behavior.

djangojs.contrib.social_authdjango_social_auth support

This module provide support for django_social_auth.

class djangojs.contrib.social_auth.SocialAuthContextMixin[source]

Bases: object

Handle django_social_auth context specifics

process_social_auth(social_auth, data)[source]

Just force social_auth’s LazyDict to be converted to a dict for the JSON serialization to work properly.

class djangojs.contrib.social_auth.SocialAuthContextSerializer(request)[source]

Bases: djangojs.contrib.social_auth.SocialAuthContextMixin, djangojs.context_serializer.ContextSerializer

Already packed django_social_auth ContextSerializer