Skip to main content

Manage Django URLs for AngularJS

Project description

==================================================== django-angular-url: Manage Django URLs for AngularJS

You may have noticed, that AngularJS controllers sometimes need a URL pointing to a Django view of your application. Do not enter into temptation to hard code such a URL into the client side controller code. Nor enter into temptation to create Javascript dynamically using a template engine. There is a clean and simple solution to solve this problem.

It is good practice to add configuration directives to applications as constants to the AngularJS module definition. This can safely be done in the template code rendered by Django, where it belongs!

Acknowledgements

This code is based on the django-angular project by Jacob Rief https://github.com/jrief/django-angular

It only includes the url reversing functionality and uses the old strategy of having the routes loaded in a Javascript object on the template.

Installation

It is assumed that your AngularJS application has already been initialized and that you have loaded django-angular tags, {% load django_angular_tags %}:

{% load django_angular_tags %}
<script>
    var my_app = angular.module('MyApp', ['ng.django.urls', /* other dependencies */]);
</script>

Now, you have to include django-angular-url.js and add data about your django url configuration:

<script src="{% static 'django_angular_url/js/django-angular-url.js' %}"></script>
<script>angular.module('ng.django.urls').constant('patterns', {% load_djng_urls %});</script>

The djangoUrl service is then available through dependency injection to all directives and controllers.

Usage

The reversing functionality is provided by djangoUrl.reverse(name, args_or_kwargs) method. It behaves much like the django's url template tag.

Parameters

name The url name you wish to reverse, exactly the same as what you would use in {% url %} template tag. args_or_kwargs (optional) An array of arguments, e.g. ['article', 4] or an object of keyword arguments, such as {'type': 'article', 'id': 4}.

Example

my_app.controller('MyCtrl', ['$scope', '$http', 'djangoUrl',
 function($scope, $http, djangoUrl) {

    $http.post(djangoUrl.reverse('api:articles', [1]), {action: 'get_data'})
        .success(function (out_data) {
            $scope.data = out_data;
    });

    // Or $http.post(djangoUrl.reverse('api:articles', {'id': 1}) ...
    // djangoUrl.reverse('api:article', {'id': 1}) returns something like '/api/article/1/'
}]);

Parametrized URL templates

djangoUrl's reverse() method also provides an option to create parametrized URL templates, which can be used with Angular's $resource. These templates look something like: /api/articles/:id/, parameters prefixed by : are filled by Angular.

You can create parametrized templates by using reverse() method in keyword arguments mode. Parameters not present in keyword arguments object will be replaced by : prefixed name from urlpatterns.

my_app.controller('MyCtrl', ['$scope', '$http', 'djangoUrl',
 function($scope, $http, djangoUrl) {
    // Urlconf
    // ...
    // url(r'^api/(?P<type>\w+)/(?P<id>\d+)/$', api.models, name='api'),
    // ...

    // djangoUrl.reverse('api', {'id': 1, 'type': 'article'}) -> /api/article/1/
    // djangoUrl.reverse('api', {'id': 1}) -> /api/:type/1/
    // djangoUrl.reverse('api', {'type': 'article'}) -> /api/article/:id/
    // djangoUrl.reverse('api', {}) -> /api/:type/:id/
    // djangoUrl.reverse('api') -> /api/:type/:id/
    // When nothing is passed as args_or_kwargs argument, reverse() defaults
    // to keyword arguments mode
}]);

So when building a service with $resource you can use djangoUrl.reverse() method just to make a parametrized URL template, or to partially fill it and have Angular add other arguments.

my_app.controller('MyCtrl', ['$resource', 'djangoUrl', function($resource, djangoUrl) {

    var Article = $resource(djangoUrl.reverse('api'), {'id': '@id', 'type': 'article'});
    // or
    var Article = $resource(djangoUrl.reverse('api', {'type': 'article'}), {id: '@id'});

}]);

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-angular-url-0.1.2.tar.gz (16.2 kB view details)

Uploaded Source

File details

Details for the file django-angular-url-0.1.2.tar.gz.

File metadata

  • Download URL: django-angular-url-0.1.2.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for django-angular-url-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d67d14f000b9f405b2810d0034932f39a1b17058efbc75aac1d95bb6b667ead5
MD5 be561e35c9b40ed9172fa33c8f8c9b94
BLAKE2b-256 0160e12f42a4d8f6e5a9ca1e9fbf14383e5bb2a9ca2aed803f69aa1867406cf8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page