views:

108

answers:

4
some_view?param1=10&param2=20

def some_view(request, param1, param2):

Is such possible in Django?

+1  A: 

I'm not sure it's possible to get it to pass them as arguments to the view function, but why can't you access the GET variables from request.GET? Given that URL, Django would have request.GET['param1'] be 10 and request.GET['param2'] be 20. Otherwise, you'd have to come up with some kind of weird regular expression to try and do what you want.

Paolo Bergantino
+1  A: 

I agree with Paolo... the stuff after the '?' are GET parameters and should probably be treated as such. That said, if you really want to keep the definition of some_view() as you've stated in the question, you could do something like:

from django.http import Http404
def some_view_proxy(request):
     if 'param1' in request.GET and 'param2' in request.GET:
         return some_view(request, request.GET['param1'],
                          request.GET['param2'])
     raise Http404

Or you could just define some_view() like this and use the GET params. Just curious, why do you want that?

uzi
I need this cause - its use less code, i can maybe make a decorator for this
dynback.com
+1  A: 

Instead of fighting Django, why not just request some_view/10/20 and then set up urls.py to extract the arguments?

Ignacio Vazquez-Abrams
+1: Use the URLs -- everything works better.
S.Lott
I am not fighting, it is my json service, jquery use params to send, many different and many not required. Thats why its easier.
dynback.com
+3  A: 

You could always write a decorator. Eg. something like (untested):

def map_params(func):
    def decorated(request):
        return func(request, **request.GET)
    return decorated

@map_params
def some_view(request, param1, param2):
    ...
Brian
Nice. I think that'd work. Very purty. :) +1
Paolo Bergantino