The Django.js library expose reverse urls to javascript. You can call the Django.url() method with:
Django.url('my-view');
Django.url('my-view', arg1, arg2);
Django.url('my-view' [arg1, arg2]);
Django.url('my-view', {arg1: 'value1', arg2: 'value2'});
Django.url('ns:my-view');
Django.url('ns:nested:my-view');
You can use anonymous forms (variable arguments and array) with named arguments in urls but you can’t use object form with anonymous arguments.
You can also force unnamed URLs serialization with settings.JS_URLS_UNNAMED:
Django.url('path.to.my.view');
Note
You can filter included urls names and namespaces by using either the settings whitelists and blacklists: settings.JS_URLS, settings.JS_URLS_EXCLUDE, settings.JS_URLS_NAMESPACES, settings.JS_URLS_NAMESPACES_EXCLUDE.
For more informations, see Settings.
You can obtain a static file url with the static or file methods:
Django.static('my-data.json');
Django.file('my-data.json');
Django.static('another/data.pdf');
Django.file('another/data.pdf');
Django.js wraps some Django values normally accessible in the template context:
In fact, any value contributed by a context processor and serializable will be accessible from Django.context.
Django.js allows you to check basic user attributes and permissions from client side. You can simply access the Django.user object or call the Django.user.has_perm() method:
console.log(Django.user.username);
if (Django.user.is_authenticated) {
do_something();
}
if (Django.user.is_staff) {
go_to_admin();
}
if (Django.user.is_superuser) {
do_a_superuser_thing();
}
if (Django.user.has_perm('myapp.do_something')) {
do_something();
}
Note
When using a custom user model with Django 1.5+, only the username and is_authenticated fields are significants. The other fields values will always be False or an empty tuple (for permissions), unless they exists on your custom model.
Django.js provides some helpers for CSRF protection.
Django.csrf_token();
Django.csrf_element();