jrief / django-angular

Let AngularJS play well with Django
http://django-angular.awesto.com/
MIT License
1.23k stars 294 forks source link

Add feature to reverse url #254

Closed stiig closed 8 years ago

stiig commented 8 years ago

Please, add feature to reverse django named url to Angular parametrized URL.

jrief commented 8 years ago

Can you please give me an example.

stiig commented 8 years ago

yes, of course

Now, i did it:

myApp.factory('myRes', ['$resource', 'djangoUrl',
    function ($resource, djangoUrl, id) {
        var url = djangoUrl.reverse('orders:order_buyer_detail');
        return $resource(url, {djng_url_args: ''}, {
            query: {method: 'GET'}
        });
    }]);

because djangoUrl.reverse('orders:order_buyer_detail') by default transform to

/angular/reverse/?djng_url_name=orders%3Aorder_buyer_detail

but i need something like that

/orders/order_buyer_detail/:pk

I know about CRUDView, but i do not need all methods from there and i have already my View method Sorry for my english. Thanks.

jrief commented 8 years ago

then try

var url = djangoUrl.reverse('orders:order_buyer_detail', {pk: 17})

17 is an example for your PK.

stiig commented 8 years ago

Yes, it's work, but it static solution. Url transform to

/angular/reverse/?djng_url_name=orders%3Aorder_buyer_detail&djng_url_kwarg_pk=17

And if i change my resource to:

myApp.factory('myRes', ['$resource', 'djangoUrl',
function ($resource, djangoUrl, id) {
        var url = djangoUrl.reverse('orders:order_buyer_detail', {pk:17});
        return $resource(url, {djng_url_kwarg_pk: ''}, {
            query: {method: 'GET'}
        });

and require myRes in my controller:

myRes.query({djng_url_kwarg_pk: id});

Request following to /angular/reverse/?djng_url_name=orders%3Aorder_buyer_detail&djng_url_kwarg_pk=17&djng_url_kwarg_pk=9 and it works

jkosir commented 8 years ago

Been thinking abot this for a while actually. Will look into it shortly.

On Thursday, April 7, 2016, Vasiliy notifications@github.com wrote:

Yes, it's work, but it static solution. Url transform to

/angular/reverse/?djng_url_name=orders%3Aorder_buyer_detail&djng_url_kwarg_pk=17

And if i change my resource to:

myApp.factory('myRes', ['$resource', 'djangoUrl', function ($resource, djangoUrl, id) { var url = djangoUrl.reverse('orders:order_buyer_detail', {pk:17}); return $resource(url, {djng_url_kwarg_pk: ''}, { query: {method: 'GET'} });

and require myRes in my controller:

myRes.query({djng_url_kwarg_pk: id});

Request following to /angular/reverse/?djng_url_name=orders%3Aorder_buyer_detail&djng_url_kwarg_pk=17&djng_url_kwarg_pk=9 and it works`

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/jrief/django-angular/issues/254#issuecomment-206813462

Lep pozdrav, Jakob Košir

jkosir commented 8 years ago

Basically angular will replace :param_name with passed kwargs or object attribute if there's one with such name.

So if we build a url like /angular/reverse/?djng_url_name=orders%3Aorder_buyer_detail&djng_url_kwarg_pk=:pk it will work ok when angular replaces :pk part with e.g. 17. But if it isn't replace we will get {'pk': ':pk'} in django middleware. I guess we could ignore params starting with :.

Then you can't use kwargs/args that start with : which shouldn't be a problem really. Thoughts?

stiig commented 8 years ago

Nice thing, but what to do if need be two or more parameters. Example: we have url in urlconf like this

r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$'

if djangoUrl would return /articles/:year/:month that would be great, but in example top i can't understand the behavior function

jkosir commented 8 years ago

Well you can actually do that with djangoUrl.reverse('articles', {year: ':year', month: ':month'}), but it will encode :'s and return '/angular/reverse/?djng_url_name=articles&djng_url_kwarg_month=%3Amonth&djng_url_kwarg_year=%3Ayear'.

So we'd have to add a parametrize option to djangoUrl.reverse call. And then also do the filtering in middleware as mentioned. I'll give this a try sometime in the next few days.

stiig commented 8 years ago

Thanks. It is great

jkosir commented 8 years ago

Added support for parametrized urls :+1:

Now you can do

var myRes = $resource(djangoUrl.reverse('orders:order_buyer_detail', {id: ':id'}));
myRes.query({id:2}); // Will call reverse('orders:order_buyer_detail', kwargs={'id':2})

// If :param isn't set it will be ignored, e.g.
myRes.query(); // Will call reverse('orders:order_buyer_detail')

// also @param stuff works
var CreditCard = $resource(djangoUrl.reverse('card', {id: ':id'}), {id: '@id'});
var card = CreditCard.get({id: 3}, function (success) {
  card.holder = 'John Doe';
  card.$save() // Will correctly POST to reverse('cards', kwargs={'id':3})
})

If you want to use it before it gets merged and released you can install parametrized-urls branch with pip: pip install https://github.com/jrief/django-angular/archive/feature/parametrized-urls.zip