On Mon, Mar 8, 2010 at 10:41 PM, Nick <nickt...@gmail.com> wrote:
> I am working on an api that outputs a list of JSON based on certain
> criteria.  Currently if someone enters 
> http://example.mysite/slideshows/api?id=1
> it returns a JSON serialized output of the slideshow with that ID.
>
> What I would like to do is allow for multiple ids, so
> http://example.mysite/slideshows/api?id=1,2,5,9,10 will pull in JSON
> values for each of those slideshows.
>
> I'd like to keep from using a third party API solution, I have checked
> them out and I like the customization with the hand built process
>
> I am doing everything through a get process in a view, here it is:
>
>
> def slideshowAPI2(request):
>    error = False
>    if 'id' in request.GET and request.GET['id']:
>        id = request.GET['id']
>        object = slideshow.objects.get(pk=id)
>        return render_to_response('slideshow.json',
>            {'object': object, 'id':id})
>    if 'year' in request.GET and request.GET['year']:
>        year = request.GET['year']
>        object = serializers.serialize("json",
> slideshow.objects.filter(publishdate__year=year))
>        html = "%s" % object
>        return HttpResponse(html)
>    else:
>        error = True
>        return render_to_response('slideshow.json', {'error': True})
>

Just a mild critique:

http://example.mysite/slideshows/api?id=1,2,5,9,10

This is not the usual or typical way to pass a list of values into a
url query string. Typically, you would specify the id argument
multiple times in the query string. This is how your browser, a JS
framework, django or anything that is designed to parse query strings
would expect to receive a list of values. Eg:

http://example.mysite/slideshows/api?id=1&id=2&id=5&id=9&id=10

In django, you can retrieve a list of values passed in a query string
like this with the getlist() method on QueryDict[1]. Eg:

if 'id' in request.POST:
  ids = request.POST.getlist('id')


Cheers

Tom

[1] 
http://docs.djangoproject.com/en/1.1/ref/request-response/#django.http.QueryDict.getlist

-- 
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.

Reply via email to