动态的SEO友好网址 [英] Dynamic SEO-friendly URLs

查看:171
本文介绍了动态的SEO友好网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以两种方式部署动态网址:

I'd like to deploy dynamic URL's for my app in two ways:


  1. 在查看可用的车辆时,我会收到一个链接: http://www.url.com/2006-Acura-MDX-技术 - 包装

  2. 我也有一个过滤器页面,所以这里,URL将根据如下选择的过滤器进行更改: http://www.url.com/2007-Nissan http://www.url.com/2007-Nissan-Maxima 等等,这取决于用户选择的过滤器。

  1. when viewing available vehicle, I get a link like: http://www.url.com/2006-Acura-MDX-Technology-Package
  2. 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 }}&nbsp;
    {{ vehicle.common_vehicle.series.model.manufacturer }}&nbsp;
    {{ vehicle.common_vehicle.series.model.model }}&nbsp;
    {{ 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

    这篇关于动态的SEO友好网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆