开发者

django routes help

开发者 https://www.devze.com 2022-12-18 15:18 出处:网络
Hay guys, im making a simple car sale website. I have a nice little urlpattern which works well as expected

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?


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)


Use SlugField (or AutoSlugField) for build SEO friendly URLs

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号