动态的SEO友好网址
我想以两种方式部署动态网址:
I'd like to deploy dynamic URL's for my app in two ways:
- 在查看可用的车辆时,我会收到一个链接: http://www.url.com/2006-Acura-MDX-技术 - 包装
- 我也有一个过滤器页面,所以这里,URL将根据如下选择的过滤器进行更改: http://www.url.com/2007-Nissan 或 http://www.url.com/2007-Nissan-Maxima 等等,这取决于用户选择的过滤器。
- 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.
最好的方法是什么?
编辑1
现在可以使用
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)
然后在我的模板中我有:
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]+)/$',....
具有与一页对应的数据库实体(例如车辆视图和车辆DB表),您可以在模型类中使用define get_absolute_url()
方法。
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.
更多 get_absolute_url
: http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url
例如:
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)
每当您使用车辆对象时,您都可以获得完整的seo-friendlyurl ..
whenever you are working with vehicle objects, you can get the full 'seo-friendly' url ...
我的过滤器的天真的方法是:
my naive approach for the filter would be:
-
在
urls.py
中编写一个合适的正则表达式,将整个字符串值传递给视图函数,以进一步调度或设计正则表达式要一致和结构化。
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