Re: call more than one view action from an uri path?
On Oct 21, 10:39 am, Steve Holden wrote: > REST, however, has a fairly rigid one-URL-one-action structure Obligatory REST nitpick: REST maps URIs to resources, not actions. In HTTP, the HTTP methods specify the action to perform. (REST as described by Fielding also has nothing whatsoever to say about what URIs should look like, but nice URIs and widespread URL conventions are so helpful to humans that nobody cares what he says ;-) ... unfortunately this also leads people to completely ignore what he says about hypertext.) - PW -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
> You could try something like: > > args = iter(args) # may need args.__iter__() in earlier Pythons? > for func in args: > arg = next(args) # may need args.next() in earlier Pythons? > ... > > > if func in list_of_allowed_funcs: > > func(arg) > > return HttpResponse('whatever') Yes, I know it won't run as written. You also can't call func(arg) directly, since func is a string, not a callable -- Python being what it is, there's a way to find the callable based on the name, but I'd have to look it up at the moment. Or you could use a dict_of_allowed_callables to look up the callable based on a string that may not be the same as the callable name. > Oops. I omitted to point out (though the OP picked it up) that a > repeated group in a regex only leaves behind its last match in the match > object's groups. It's all going to get a bit painful trying to do this > with a urlconf. Ah, there's something I didn't know. There must be *some* way to get all matches from a Python regex, though, isn't there? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
> Well, like I said, I haven't tested that particular regex. But it > should be possible to write a regex that does work. it turns out your regex works fine: print re.search(r'^((\w+)/(\d+)/)+$', 'fries/4/frobs/9/areas/ 2/').group(1) Django uses groups() instead of group(1), for whatever reason. A-splitting I go! Tx all! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
On 10/21/2010 2:24 PM, ringemup wrote: > #urls.py > #note: untested regex, but a regex should be able to do this > url('^((\w+)/(\d+)/)+$', 'myview', ...) > Oops. I omitted to point out (though the OP picked it up) that a repeated group in a regex only leaves behind its last match in the match object's groups. It's all going to get a bit painful trying to do this with a urlconf. regards Steve -- DjangoCon US 2010 September 7-9 http://djangocon.us/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
On 10/21/2010 2:24 PM, ringemup wrote: > You can't do something like this? > > #urls.py > #note: untested regex, but a regex should be able to do this > url('^((\w+)/(\d+)/)+$', 'myview', ...) > > #views.py > # note: this is pseudocode > def myview(request, *args, **kwargs): > # iterate through args two at a time > for func, arg in args: This, sadly, does not iterate over the arguments two at a time. >>> for a, b in [1, 2, 3, 4]: ... print a, b ... Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable >>> But you said it was pseudocode, so you are forgiven. You could try something like: args = iter(args)# may need args.__iter__() in earlier Pythons? for func in args: arg = next(args) # may need args.next() in earlier Pythons? ... > if func in list_of_allowed_funcs: > func(arg) > return HttpResponse('whatever') > regards Steve -- DjangoCon US 2010 September 7-9 http://djangocon.us/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
Well, like I said, I haven't tested that particular regex. But it should be possible to write a regex that does work. If Django's dispatcher chokes on subpatterns, then just use the ^(.+)$ approach and split it yourself in the view, then iterate. Either way, you shouldn't have to call multiple functions directly from the URLconf. On Oct 21, 2:38 pm, Phlip wrote: > > url('^((\w+)/(\d+)/)+$', 'myview', ...) > > Actually, no, that's only giving the last two matches: > > (u'areas/2/', u'areas', u'2') -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
> url('^((\w+)/(\d+)/)+$', 'myview', ...) Actually, no, that's only giving the last two matches: (u'areas/2/', u'areas', u'2') -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
> You can't do something like this? > url('^((\w+)/(\d+)/)+$', 'myview', ...) Ah, I started with ^(.*)$ , and put the split on the inside. I will go with your + pattern, because it checks for trivial errors at the correct level. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
You can't do something like this? #urls.py #note: untested regex, but a regex should be able to do this url('^((\w+)/(\d+)/)+$', 'myview', ...) #views.py # note: this is pseudocode def myview(request, *args, **kwargs): # iterate through args two at a time for func, arg in args: if func in list_of_allowed_funcs: func(arg) return HttpResponse('whatever') On Oct 21, 1:50 pm, Phlip wrote: > > REST, however, has a fairly rigid one-URL-one-action structure which is > > ideally suited to Django's URL dispatch. The only way to get what you > > want is to layer another dispatching service atop Django's. I don't > > think you can do it with urlconfs alone. > > knp. The point of urlconf is to adapt arbitrarily complex URL paths > via regexp. > > If we only need very stereotypical url paths, we can easily parse > the / marks ourselves. I just didn't want to reinvent any wheel if > there were, for example, a method in url-land we could override. > > > By the way, what are you imagining the functions nest(), pest() and > > rest() will return? They cannot all return HTTP responses, since there > > can only be a single response to a single request? > > Point. The NeoUrlConf will call each argument to an argument handler, > then pass all three results into a terminal handler that cooks HTTP. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
> REST, however, has a fairly rigid one-URL-one-action structure which is > ideally suited to Django's URL dispatch. The only way to get what you > want is to layer another dispatching service atop Django's. I don't > think you can do it with urlconfs alone. knp. The point of urlconf is to adapt arbitrarily complex URL paths via regexp. If we only need very stereotypical url paths, we can easily parse the / marks ourselves. I just didn't want to reinvent any wheel if there were, for example, a method in url-land we could override. > By the way, what are you imagining the functions nest(), pest() and > rest() will return? They cannot all return HTTP responses, since there > can only be a single response to a single request? Point. The NeoUrlConf will call each argument to an argument handler, then pass all three results into a terminal handler that cooks HTTP. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
On 10/21/2010 9:36 AM, Phlip wrote: > On Oct 21, 5:26 am, Scott Gould wrote: > >> What's your use case? Are "nest, pest and rest" always "nest, pest and >> rest" -- or could they be "rest, pest and nest", or "nest, best, and >> rest"? > > Tx but - the use case is we can't do it like you said. C-: > > The point is a REST path that can go arbitrarily shallow or deep > without excessive code. > REST, however, has a fairly rigid one-URL-one-action structure which is ideally suited to Django's URL dispatch. The only way to get what you want is to layer another dispatching service atop Django's. I don't think you can do it with urlconfs alone. > What I looked for was a lambda here (simplified): > > url( 'nest', lambda *a,**k: doit(a, k) ) > > but that terminates the lookup on the last item (rest), instead of > calling the lambda on the way down! > By the way, what are you imagining the functions nest(), pest() and rest() will return? They cannot all return HTTP responses, since there can only be a single response to a single request? Perhaps a somewhat more detailed description of your requirements is in order. regards Steve -- DjangoCon US 2010 September 7-9 http://djangocon.us/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
On Oct 21, 5:26 am, Scott Gould wrote: > What's your use case? Are "nest, pest and rest" always "nest, pest and > rest" -- or could they be "rest, pest and nest", or "nest, best, and > rest"? Tx but - the use case is we can't do it like you said. C-: The point is a REST path that can go arbitrarily shallow or deep without excessive code. What I looked for was a lambda here (simplified): url( 'nest', lambda *a,**k: doit(a, k) ) but that terminates the lookup on the last item (rest), instead of calling the lambda on the way down! > > -- > > Phlip > > http://bit.ly/ZeekLand -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: call more than one view action from an uri path?
What's your use case? Are "nest, pest and rest" always "nest, pest and rest" -- or could they be "rest, pest and nest", or "nest, best, and rest"? If they're set in stone; that is, it's always nest, followed by pest, followed by rest, then it's easy enough to just parameterize the url: '/nest/(\d+)/pest/(\d+)/rest/(\d+)/' and have your view delegate like so: #using session def myview(request, x, y, z): nest(x) pest(y) rest(z) #using return values def myview(request, x, y, z): nest(x, pest(y, rest(z))) Even if the functions are variable, it should be easy to enough to extend this approach accordingly. On Oct 20, 6:51 pm, Phlip wrote: > Djangoistas: > > We want to call this URL: > > http://www.server.com/nest/5/pest/6/rest/7 > > We want each handler to call, with its argument, in order: > > def nest(request, x): > ... > def pest(request, y): > ... > def rest(request, z): > ... > > (Internally, at rest() time, we want x and y to be available on some > session or file-scope variable.) > > How to write an url() call in urls.py that handles part of a path, and > then dispatches to the next part of the path, if any? > > -- > Phlip > http://bit.ly/ZeekLand -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.