customize url for GET form

32 views
Skip to first unread message

jean polo

unread,
Sep 3, 2010, 9:20:36 AM9/3/10
to Django users
hello
while using GET form, I can select several objects with an URL like:
xxxxxx?id=1&id=2&id=3 etc..

How could I have instead an URL like that:
xxxxxx?id=1,2,3,4 etc... ??

Thanks,
_j

Alexandre González

unread,
Sep 3, 2010, 9:26:30 AM9/3/10
to django...@googlegroups.com
I think that if you use id=1&id=2&id=3 you are overriding the id value... It's no way to do that with GET


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Please, don't send me files with extensions: .doc, .docx, .xls, .xlsx, .ppt and/or .pptx

Daniel Roseman

unread,
Sep 3, 2010, 9:26:02 AM9/3/10
to Django users
Why would you want to? The first is the standard HTTP method of
sending multiple items in a GET (and indeed in a POST, although you
don't see the data in the URL). Changing it would probably be a bad
idea.
--
DR.

jean polo

unread,
Sep 3, 2010, 9:45:24 AM9/3/10
to Django users
well it was just to simplify some URLs but if you guys tell it's bad
practice, I'll stick with it =)

cheers,
_j

Tom Evans

unread,
Sep 3, 2010, 9:46:25 AM9/3/10
to django...@googlegroups.com
2010/9/3 Alexandre González <agonz...@gmail.com>:

> I think that if you use id=1&id=2&id=3 you are overriding the id value...
> It's no way to do that with GET
>

Au contraire:

>>> from django.http import QueryDict
>>> QueryDict('id=1&id=2&id=3')
<QueryDict: {u'id': [u'1', u'2', u'3']}>


Cheers

Tom

Alexandre González

unread,
Sep 3, 2010, 9:55:29 AM9/3/10
to django...@googlegroups.com
Wel... but to do this, you must to catch the url and get from it... but really, with urls.py can you do that?

Perhaps, with a regex line in urls.py as:

(^\d+)?(/\d+)*/?$ you can get all the number style: http://url/1/2/3/4/5... but I don't know how do it exactly.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

jean polo

unread,
Sep 3, 2010, 12:05:02 PM9/3/10
to Django users
ok sorry if I used a bad example with id..
I meant something like:
xxxxxx?bla=1,2,3,4

I got it working by using a special field ('bla') in my form which is
a CharField
then I parse bla:
arr = request.GET.get('bla').split(',')
if all items of arr are integers, I have my array of 'bla' =)

cheers,
_y


On Sep 3, 3:55 pm, Alexandre González <agonzale...@gmail.com> wrote:
> Wel... but to do this, you must to catch the url and get from it... but
> really, with urls.py can you do that?
>
> Perhaps, with a regex line in urls.py as:
>
> (^\d+)?(/\d+)*/?$ you can get all the number style:http://url/1/2/3/4/5...
> but I don't know how do it exactly.
>
>
>
> On Fri, Sep 3, 2010 at 15:46, Tom Evans <tevans...@googlemail.com> wrote:
> > 2010/9/3 Alexandre González <agonzale...@gmail.com>:
> > > I think that if you use id=1&id=2&id=3 you are overriding the id value...
> > > It's no way to do that with GET
>
> > Au contraire:
>
> > >>> from django.http import QueryDict
> > >>> QueryDict('id=1&id=2&id=3')
> > <QueryDict: {u'id': [u'1', u'2', u'3']}>
>
> > Cheers
>
> > Tom
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com<django-users%2Bunsu...@googlegroups.com>
> > .

Tom Evans

unread,
Sep 3, 2010, 12:39:25 PM9/3/10
to django...@googlegroups.com
On Fri, Sep 3, 2010 at 5:05 PM, jean polo <josia...@googlemail.com> wrote:
> ok sorry if I used a bad example with id..
> I meant something like:
> xxxxxx?bla=1,2,3,4
>
> I got it working by using a special field ('bla') in my form which is
> a CharField
> then I parse bla:
> arr = request.GET.get('bla').split(',')
> if all items of arr are integers, I have my array of 'bla' =)
>
> cheers,
> _y
>

Danger Will Robinson!

You are now departing from convention, or how everyone else does
things. Whilst this may work for a while, you will quickly get annoyed
that you cant do simple things easily, like generating URLs of that
format.

If your URL looks like this:

xxxxx?bla=1&bla=2&bla=3&bla=4

then you get your list of bla like so:

arr = request.GET.getlist('bla')

You can then regenerate your URL easily using a QueryDict:

>>> from django.http import QueryDict
>>> q=QueryDict('', mutable=True)
>>> q.setlist('blah', ['1', '2', '3', '4'])
>>> q.urlencode()
'blah=1&blah=2&blah=3&blah=4'

However, you now can't do any of this, and will have to hand craft
your URLs, just because you don't like convention. Enjoy.


Cheers

Tom

jean polo

unread,
Sep 3, 2010, 2:24:13 PM9/3/10
to Django users
ah ok, I was expecting something like that ..
this is only for a *very* special case though, not for my normal form
thingz..
thanks for all the advices !

cheers,
_y


On Sep 3, 6:39 pm, Tom Evans <tevans...@googlemail.com> wrote:
Reply all
Reply to author
Forward
0 new messages