New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New method for reversing django's urls in angular #144
Conversation
…g with own method
That looks awesome! I've not yet fully understood how to inject Django URL's into Angular, without adding a code snippet rendered by a Django template. |
Just have a look at DjangularUrlResolverView, should be pretty straightforward. It doesn't inject anything actually, just passes the arguments for |
Added the documentation, preview build available here. I think this is ready to be merged now. |
This is a good idea. It's definitely not safe to have all django registered address in the javascript source. |
Small change: |
If in the middleware, how do you control who can access this function? What if i want to serve different url for different users? I like the view approach better. Maybe this could be a choice? |
Which function? |
I mean the DjangularUrlResolverView. |
When calling your view, it's decorated with all middlewares (session, csrf protection etc. ). The function by itself doesn't really "do" anything, it just starts the same process as if the request came for already reversed url. I don't exactly see the point of reversing the url to different views for different users, this is supposed to mimic the |
@dasf However, I'm not really convinced that we shall do this in the middleware. How about adding a urlpattern, which shall be included into the main projects Another approach would be to add a simple Ajax getter, which converts the Django URL namespace on the server. The new problem we now have, is that Django's
does not return any ResolverMatch object. Another note on the middleware settings.I would rename Are you sure, that if you use |
Yes, initially this was done with a view. The only reason I changed it to middleware is because it's easier to configure, no need for Not exactly sure what you mean with ajax getter? Yes, it should be renamed to And yes, this should always be the first middleware. Whatever |
Because urls.py is where customers expect to have a URL parsed. Otherwise its some kind of magic, and thats bad. Even Django's Therefore, if its not for a technical reason, I would prefer to add it to urls.py. This way a customer can even configure the prefix. |
urlconf routes request to the view, while a middleware does some sort of request (or response) processing. The url resolving essentially modifies the request path so it's routed to the correct view, which makes sense to be done before (and without) resolving the
Adding an option to change prefix makes sense, but I don't think that's a desired functionality in most cases. Also it would require changing the prefix in |
Just added an alternative implementation using Add to your
This is based on your excellent work done for the middleware class. Of course we'd have to add a configuration option to the Javascript part. Personally I'd like to get rid of the term "djangular". This was a bad choice, how about naming this special URL I really dislike any magic things happening in a middleware class before any URL routing. Zen of Python (PEP 20) states:
and
so lets not break these rules. What do you think? |
Well ok then, let's do it with a view. I see you added a commit with view approach, but haven't changed the middleware. This will break csrf and some other middlewares in some cases. |
This was just for demonstration purpose. If you agree, we shall remove your middleware class and focus on the explicit |
You can't remove the middleware class. It has to break middleware processing and that can't be done in a view. Without middleware you can either have the csrf verifications fail (possibly also some other middleware stuff) or bypass authentication and security checks for every view. 5ff839d has the view + middleware approach, use that if you want to do url resolving in a view. Then all it needs is a |
Have you decided which implementation we should use? |
They currently work both. I'd like to do some more tests. If my router would allow to bypass permission settings, then of course this is a no-go. On the other side, I don't like magic... But I agree, that it was a bad idea to merge that PR too early. I turned the docs back to tag 0.7.10, so at least we don't confuse anybody. Sorry for the delays. |
No problem with the delays :) Well whatever you do, you have to break middleware processing (and that can only be done with a custom middleware class), otherwise |
At the moment I am using this middleware in my current project. Unfortunately I'm having some trouble with URL reversing in combination with ViewSets from DRF. Therefore I'd like to make another implementation proposal, which for sure will not have the problems with To django-angular, we add a special view which does nothing else than converting the named URL patterns into a real URL and return it to the client. This real URL then is used by our Angular What do you think about? Do you have any security concern, if everybody can |
What problems? I also used this with a That implementation has an extra delay (and an extra request) albeit a small one, which is completely unnecessary. I don't agree with using that. Don't have any security concerns. |
@dasf now, I finally made it. The main changes to your PR is that I moved the reversing path from I also reorganized reverse-urls.rst a little bit. Please annotate. Then I added my alternative implementation which uses a HTTP permanent redirect onto the real URL. It is compatible with yours and so one can switch between them seamlessly. I still don't feel comfortable with the middleware approach, but I must admit that presumably it is the best solution. Please have a try. I'd like to release it as 0.7.11 so that our user base gets two intermediate versions before removing the old resolver. |
Looks good to me. 0.7.11 also makes sense. |
Hello, I'm still testing the new reverse function. I'm using the middleware my_function = function(){
var urlapi=djangoUrl.reverse("my_api_endpoint",{})+'?param1='+$scope.param1;
$http.get(urlapi).success(function(data){
}).error(function(data, status, headers, config) {
swal("Server error");
});
} The api is made with django rest framework:
i'm getting this error on the server: Internal Server Error: /angular/reverse/ This method used to work with 0.7.10. How do i make api request with parameters with the new djangoUrl.reverse function? Thanks, |
@jkosir I therefore looked for a simpler solution. Please have a look at this alternative implementation: It works perfectly in my environment, but it might have other drawback, I haven't thought about. @papasax would be great if you also could test this. I even believe that angular-reverse-requests should be a little bit faster. |
This looks much better, nice. I'll give it a try later today.
This removes the query parameters right? While |
I just tried on my project, and i have a problem with my api call. with in python gives this object <QueryDict: {u'djng_url_name': process_request in middleware.py doesn't detect the parameters because it 2015-05-11 9:53 GMT+02:00 Jakob Košir notifications@github.com:
|
@papasax |
Yes it's working now. It looks good! 2015-05-11 22:11 GMT+02:00 Jacob Rief notifications@github.com:
|
@jkosir |
Looks good to me. Will add some tests to check if it keeps arguments when I have time, hopefully tomorrow. |
OK, "simpler middleware" now is part of master branch, but I'll wait for your tests before releasing the next version. Thanks! |
Updated the tests for new approach. As far as I'm concerned this can be released now. |
PR to discuss the new method.
Basically builds a url with arguments for reverse call, DjangularUrlResolverView parses them, calls the actual view and returns the result.
djangoUrl
service, completely backwards compatibleSome other points to consider:
urls_by_namespace
function, which was already deprecated withload_djng_urls
approach, I suggest we remove that when moving to the new method.