动态 SEO 友好 URL

发布于 2024-08-14 03:33:37 字数 1478 浏览 3 评论 0原文

我想通过两种方式为我的应用程序部署动态 URL:

  1. 在查看可用车辆时,我会收到如下链接: http://www.url.com/2006-Acura-MDX-Technology-Package
  2. 我也有一个过滤器页面,所以在这里,URL会根据所选的过滤器而改变例如:http://www.url.com/2007-Nissanhttp://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 }}&nbsp;
    {{ vehicle.common_vehicle.series.model.manufacturer }}&nbsp;
    {{ vehicle.common_vehicle.series.model.model }}&nbsp;
    {{ 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:

  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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-08-21 03:33:37

如果您有一个对应于一页的数据库实体(例如车辆视图和车辆数据库表),您可以在模型类中使用定义 get_absolute_url() 方法。

有关 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友好” url ...


我对过滤器的天真的方法是:

  • 在 urls.py 中编写适当的正则表达式,要么将整个字符串值传递给视图函数以进一步分派,要么设计正则表达式保持一致和结构​​化..

    (r'^filter/(?P<名称>[a-zA-Z]+)/(?P<年份>\d+)/(?P<类型>\d+)/$) ', ...
    
  • < p>进行适当的数据库查询

  • 显示..

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 ..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文