0

Let's say I have a url which captures several argument. E.g:

url(r'^(?P<l_slug>[-\w]+)/(?P<t_slug>[-\w]+)/(?P<event_id>\d+)/$', 'views.SomeView'),

And in some cases, SomeView needs only one of the argument: E.g:

def SomeView(request, event_id):
    return HttpResponse('hi {}'.format(event_id))

The way I have been handling this issue is:

def SomeView(request, l_slug=None, t_slug=None, event_id=None):
    return HttpResponse('hi {}'.format(event_id))

Is there some what to limit what arguments are passed to the view from the url line?

GordonsBeard
  • 636
  • 4
  • 14
Cole
  • 2,489
  • 1
  • 29
  • 48

1 Answers1

3

Your only limitation would be the length of the querystring (as far as I am aware), which varies by browser. See this Stack Overflow question for more details.

As far as making the URL more readable, or extracting only the args you need, that's a bit subjective.

If you want your URL to explicit, you could continue to do what you're doing, by passing in named parameters, but you could alter your method signature to simply:

def some_view(request, *args, **kwargs):
    # more code here

and then retrieve the arg or kwarg you need, returning None if it's not present.

You can also pass the same values as querystring parameters. If you did that, you won't have to define any args or kwargs in your view at all, you would simply pull the values from the request.GET dictionary or return None if the key isn't found.

So, it's really up to you as to what's more readable.

Community
  • 1
  • 1
Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • My question isn't about the length of the querystring. It's about passing fewer arguments than are found in the querystring. What if the querystring provides several agruments - for the sake of url readability - but I only want one of those several arguments? – Cole Feb 27 '13 at 19:05
  • 1
    I've augmented my answer with some more ideas. – Brandon Taylor Feb 27 '13 at 19:11
  • Thanks for the extra ideas. It has given me a good sense of direction! – Cole Feb 27 '13 at 20:06