动态 SEO 友好 URL
我想通过两种方式为我的应用程序部署动态 URL:
- 在查看可用车辆时,我会收到如下链接: http://www.url.com/2006-Acura-MDX-Technology-Package
- 我也有一个过滤器页面,所以在这里,URL会根据所选的过滤器而改变例如:http://www.url.com/2007-Nissan 或 http://www.url.com/2007-Nissan-Maxima 等等,具体取决于过滤器用户已选择。
解决这个问题的最佳方法是什么?
编辑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)
然后在我的模板中我有:
<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>
剩下的就是将库存编号传递到详细信息视图...到目前为止我已经这样做了:
(r'^inventory/details/(?P<stock_number>[-\w]+)/$',....
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)
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]+)/
,....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个对应于一页的数据库实体(例如车辆视图和车辆数据库表),您可以在模型类中使用定义 get_absolute_url() 方法。
有关
get_absolute_url
的更多信息: http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url例如:
每当您使用车辆对象时,您都可以获得完整的“seo友好” url ...
我对过滤器的天真的方法是:
在 urls.py 中编写适当的正则表达式,要么将整个字符串值传递给视图函数以进一步分派,要么设计正则表达式保持一致和结构化..
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-urle.g:
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 ..make the appropriate DB queries