API¶
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)
.
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
-
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.
- If test are run in verbose mode (
-
phantomjs_runner
= None¶ mandatory path to the PhantomJS javascript runner
-
run_suite
()[source]¶ Run a phantomjs test suite.
phantomjs_runner
is mandatory.- Either
url
orurl_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.
Note
Template is rendered using a modified static storage that give
file://
scheme URLs. To benefits from it, you have to use either thestatic
template tag or one the djangojs template tags.Warning
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.
djangojs.urls_serializer
– URLs serialization handling¶
djangojs.utils
– Miscellaneous helpers¶
This modules holds every helpers that does not fit in any standard django modules.
-
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)[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
djangojs.tap
– Tap format parser¶
This module provide test runners for JS in Django.
djangojs.templatetags.js
– Javascript template tags¶
Provide template tags to help with Javascript/Django integration.
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
Include Django.js javascript library in the page
Include Django.js javascript library initialization in the page
A simple shortcut to render a
script
tag to a static javascript file
A shortcut to render a
script
tag for the packaged jQuery
A simple shortcut to render a
script
tag to a static javascript file
Renders verbatim tags
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:
- Miguel Araujo: https://gist.github.com/893408
djangojs.context_processors
– Context processors¶
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_auth
– django_social_auth support¶
This module provide support for django_social_auth.
Bases:
object
Handle django_social_auth context specifics
Just force social_auth’s LazyDict to be converted to a dict for the JSON serialization to work properly.
Bases:
djangojs.contrib.social_auth.SocialAuthContextMixin
,djangojs.context_serializer.ContextSerializer
Already packed django_social_auth ContextSerializer