passing additional parameters to a view

1 view
Skip to first unread message

patrick kranzlmüller

unread,
Jan 17, 2006, 8:59:19 AM1/17/06
to django...@googlegroups.com
my url looks like:
http://mysite.com/manage/projects/1/milestones/?responsible__id__exact=1

in the user-group, i found a posting where adrian said:
"The query parameters are, essentially, the same as keyword arguments
that can be passed to get_list(). "

can anybody give me a hint on how to accomplish that?

i tried

def milestones_overview(request, project_id):
milestones_list = milestones.get_list(request.GET)

... but that didn´t work.

thanks,
patrick

scum

unread,
Jan 17, 2006, 9:29:27 AM1/17/06
to Django users
All get variables are automagically placed into the request.GET
dictionary. In your example:
http://mysite.com/manage/projects/1/milestones/?responsible__id__exact=1
Your variable `responsible__id__exact` would be obtained through the
code:
myVar = request.GET["responsible__id__exact"]
or
myVar = request.GET.get("responsible__id__exact", None)

The second method is recommended because it will set myVar to None if
the GET variable is not specified.

Hope this helps.

patrick kranzlmüller

unread,
Jan 17, 2006, 9:48:22 AM1/17/06
to django...@googlegroups.com
thanks for answering.

actually, i don´t really know my query parameters.
could be responsible__id__exact=5
or status__exact=1
or responsible__id__exact=5&status__exact=5
... and so on.

so, it´d be nice to have something like milestones_list =
milestones.get_list(x), where x is the query.

thanks,
patrick

Andreas Stuhlmüller

unread,
Jan 17, 2006, 9:53:34 AM1/17/06
to django...@googlegroups.com
On 1/17/06, patrick kranzlmüller <pat...@vonautomatisch.at> wrote:
> actually, i don´t really know my query parameters.
> could be responsible__id__exact=5
> or status__exact=1
> or responsible__id__exact=5&status__exact=5
> ... and so on.
>
> so, it´d be nice to have something like milestones_list =
> milestones.get_list(x), where x is the query.

milestones_list = milestones.get_list(**request.GET) should work.

Andreas

patrick kranzlmüller

unread,
Jan 17, 2006, 10:06:00 AM1/17/06
to django...@googlegroups.com
unfortunately, it doesn´t.

scum

unread,
Jan 17, 2006, 10:14:02 AM1/17/06
to Django users
Okay- that is a lot trickier. Here's some yucky code that uses the exec
command to accomplish the same goal... but it's definitely not the
elegant solution you are looking for...

q = []
for key, value in request.GET.items():
q.append ( "%s = %s" % (key, value) )
q = ", ".join(q)
print q
cmd = "milestones_list = milestones.get_values(%s)" % q
exec cmd

scum

unread,
Jan 17, 2006, 10:16:29 AM1/17/06
to Django users
What error are you getting when you try
"milestones.get_list(**request.GET)"?

Andreas Stuhlmüller

unread,
Jan 17, 2006, 10:18:27 AM1/17/06
to django...@googlegroups.com
On 1/17/06, patrick kranzlmüller <pat...@vonautomatisch.at> wrote:
> unfortunately, it doesn´t.

You're right. You have to consider that request.GET and request.POST
are MultiValueDicts. If you don't expect one key to have more than one
value, something like this might work:

milestones_list = milestones.get_list(
**dict([(k, v[0]) for (k, v) in request.GET.items()])
)

Andreas

patrick kranzlmüller

unread,
Jan 17, 2006, 10:21:43 AM1/17/06
to django...@googlegroups.com
hmm, i thought this is quite easy as it´s already used in the
admin-interface (with filters).
thanks for the code,

patrick

Adrian Holovaty

unread,
Jan 17, 2006, 10:19:56 AM1/17/06
to django...@googlegroups.com

UGH! Do *not* use that code.

params = dict(request.GET.items())
milestones.get_values(**params)

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

patrick kranzlmüller

unread,
Jan 17, 2006, 10:35:57 AM1/17/06
to django...@googlegroups.com
since my server is down again, i can´t verify the code.
anyway, thanks everybody for your help.

patrick

scum

unread,
Jan 17, 2006, 10:37:37 AM1/17/06
to Django users
I'm having partial success with the code given here. Let me clarify
the two methods I am using.

Method 1:
leads.get_list(**request.GET)

Method 2:
params = dict(request.GET.items())
milestones.get_values(**params)


Method 1 works with keywords that want lists. For example.. the order
by clause works in method 1 but not in method 2. (?order_by=city)

Method 2 works with keywords that do not require a list. For example,
the limit and offset keywords work in method 2 and do not work in
method 1. (?offset=2&limit=1)

I believe this is where some confusion is setting in. Could someone
clarify how to write a url that one of these methods support.

James Bennett

unread,
Jan 17, 2006, 10:43:50 AM1/17/06
to django...@googlegroups.com
On 1/17/06, scum <scumi...@gmail.com> wrote:
> Method 1 works with keywords that want lists. For example.. the order
> by clause works in method 1 but not in method 2. (?order_by=city)
>
> Method 2 works with keywords that do not require a list. For example,
> the limit and offset keywords work in method 2 and do not work in
> method 1. (?offset=2&limit=1)

I'd like to go on record as saying I don't like either one of them;
letting a user decide what queries you're going to run by passing in
GET parameters just gives me the willies.


--
"May the forces of evil become confused on the way to your house."
-- George Carlin

Reply all
Reply to author
Forward
0 new messages