dotty
dotty

Reputation: 41523

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?

Upvotes: 1

Views: 774

Answers (2)

Vadim Chernysh
Vadim Chernysh

Reputation: 143

Use SlugField (or AutoSlugField) for build SEO friendly URLs

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 600051

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)

Upvotes: 10

Related Questions