tags:

views:

160

answers:

2
+1  Q: 

django routes help

Hay guys, im making a simple car sale website. I have a nice little urlpattern which works well as expected

/car/1/

goes to a car with that id, simple?

However, for SEO reasons i want to add extra data to the URL, but ignore it.

/car/1/ford/focus

as an example, how would i go about modifying my patters to take the extra parts into consideration?

the 2 patterns would go to the same place and load up the same view.

Any ideas?

+6  A: 

This won't help your SEO - in fact it will actively hurt it. If /car/1/, /car/1/ford/, /car/1/ford/focus/ all go to the same URL, you have actually decreased your search equity for that page. This is a very bad idea.

If you really want to do it, it's very simple:

r'^car/(?P<car_id>\d+)/.*/$'

but I really wouldn't do this. A much better idea is to leave out the ID and use the make and model to get the car:

r'^car/(?P<make>\w+)/(?P<model>\w+)/$'

So now you have URLs in the form /car/ford/focus/, and in your view you can do:

def myview(request, make, model):
    car = Car.objects.get(make=make, model=model)
Daniel Roseman
+1 for the SEO advice
kb
I agree, much better to leave out the id and create an accessible URL - see http://www.ibm.com/developerworks/library/us-cranky8.html for more info
Frozenskys
Thanks, very good idea.
dotty
I get this error Caught an exception while rendering: Incorrect integer value: 'ford' for column 'make_id' at row 1
dotty
I don't know how you're storing make and model values on your Car model, so I just used charfields. If they're actually foreignkeys, you'll need to alter that `.get` accordingly - see the documentation for how to do lookups across relations. If you still can't work it out, posta new question and include your models code.
Daniel Roseman
+1  A: 

Use SlugField (or AutoSlugField) for build SEO friendly URLs

Vadik
I have actually moved onto this method
dotty