views:

437

answers:

3

Hi,

I want to stay way from GET params. Don't want to use POST and I have at least two different categories to build the URL for.

The visitors are first asked to choose a location wich can be one of, for example: http://foo.com/United-States/ || http://foo.com/United-States/California/ || http://foo.com/United-States/California/San-Francisco-Region/ || http://foo.com/United-States/California/San-Francisco/

Once a location is selected, then they can pick a category which can be one of for example:

http://foo.com/Electronics/ || http://foo.com/Electronics/Camera/ || http://foo.com/Electronics/Camera/Digital/ || http://foo.com/Electronics/Camera/Digital/SLR/

So, how would I go about combining both of the above URL in one, once they are done with selecting the location and the category?

I might need to pass in the page number for pagination. (http://foo.com/page/2/)

I'd like to keep the URL clean and self explanatory. I know how to do one type of URL at a time but not combining multiple types.

If I were to do a GET, then I would be doing: http://foo.com/?locid=23323&catid=335&page=2, but I like to take advantage of Django's clean URL and stay way from the ?& stuff.

Thanks,

VN44CA

A: 

I think that this would be to many information in the URL. I assume that any location has its unique ID, the same goes to category. Why not build a URL:

http://foo.com/United-States/Electronics/

http://foo.com/California/Digital/

http://foo.com/San-Francisco/SLR/

and so on...

2 arguments are enough in your case. Or you can change category name to be more meaningful.

http://foo.com/Electronics/Camera/Digital/ => http://foo.com/Digital-Cameras/

Pawel
Thanks Pawel.Can you give me the url example for two filed urls? something that you would put in the url.py of the application. Something like this I suppose?url(r'^(?P<location>[^/]+)/(?P<item>[^/]+)/$', views.item_by_location),
VN44CA
Yes exactly, I am not an expert in django (tested it but never used it for real project). I think it will work well for SEO, because the url will not be bloated.
Pawel
A: 

Depending on how you are using the location data, and how often a user will want to change the location, it may be best stored in the session instead of having it specified in the URL.

For example, I am unlikely to start off looking for SLR digital cameras in San Francisco and then go looking for basketballs in Baltimore.

Obviously this won't be sufficient if you want permalinks to any location-category combination, though.

henrym
VN44CA
A: 

After thinking about this for a while, I found that the best solution (for me) to this is to have a string just hold all the arguments that I need in a clean URL.

So, when users first come to http://foo.com/ they are presented with a locations to select. Cookies are empty at this point.

So, a user goes ahead and selects United-State, the URL will look like http://foo.com/12334_0_0_0/United-States/ and the location cookie is set to 1234 which is the id for location United States.

Now the user selects California and the URL changes to http://foo.com/1235_0_0_0/United-States-California/ and cookie is replace from United states to 1235 which is the id for the location California. At this point the user selects Category Electronics. so the URL changes to http://foo.com/1235_3333_0_0/Electronics/ and 3333 is saved in the Category Cookies.

If the users drops down to SLR Camera, then s/he'll see http://foo.com/1235_3344_0_0/SLR-Cameras/.

This way, the first portion of the URL keeps track of up to 4 arguments which can be passed around and the names (slugs) are merely for presentation and SEO.

I think this would work right? It would be cool to have the x_y_z_p portion of the URL encoded into some random text and decode back into args & numbers.

Let me know what you do think?

VN44CA
You shouldn't make http://foo.com/12334_0_0_0/. This should work fine http://foo.com/12334/ unless you need to know that 12334 is x and not for example y (in that case you can make http://foo.com/0_12334/ or even http://foo.com/_12334/). The IDs are meaningless so try not to bloat the ID part of URL. Also I would change underscore to comma (but it is totally matter of your preferences)
Pawel
And... one more thing. Why you want to encode the URL to random text?
Pawel