Dynamic SEO-friendly URLs
I'd like to deploy dynamic URL's for my app in two ways:
- when viewing available vehicle, I get a link like: http://www.url.com/2006-Acura-MDX-Technology-Package
- I also have a filter page, so here, the URL will change according to the filters selected like: http://www.url.com/2007-Nissan or http://www.url.com/2007-Nissan-Maxima and so on depending on the filters the user has chosen.
What's the best way to go about this?
EDIT 1
This now works
def get_absolute_url(self):
return u'%s-%s-%s-%s-%s' % (self.common_vehicle.year.year,
self.common_vehicle.series.model.manufacturer,
self.common_vehicle.series.model.model,
self.common_vehicle.series.series,
self.stock_number)开发者_JS百科
Then in my template I have:
<a href="{{ vehicle.get_absolute_url }}/">
<span class="vehicle-title">
{{ vehicle.common_vehicle.year.year }}
{{ vehicle.common_vehicle.series.model.manufacturer }}
{{ vehicle.common_vehicle.series.model.model }}
{{ vehicle.common_vehicle.series.series }}
</span>
</a>
All that remains is getting the stock number passed to the details view...so far I've done it like so:
(r'^inventory/details/(?P<stock_number>[-\w]+)/$',....
if you have a database entity corresponding to one page (e.g. vehicle view and a Vehicle DB table), you can use define get_absolute_url()
method in the model class.
more on get_absolute_url
: http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url
e.g:
class Vehicle(models.Model):
name = ...
year = ...
fancy_stuff = ...
def get_absolute_url(self):
return u'%s-%s-%s' % (self.year, self.name, self.fancy_stuff)
whenever you are working with vehicle objects, you can get the full 'seo-friendly' url ...
my naive approach for the filter would be:
write an appropriate regex in
urls.py
, either passing on a whole string value to a view function for further dispatch or designing the regex to be consistent and structured ..(r'^filter/(?P<name>[a-zA-Z]+)/(?P<year>\d+)/(?P<type>\d+)/$)', ...
make the appropriate DB queries
- display ..
精彩评论