在网站中实现 URL slug 功能
我想知道如何在网站中实现 URL slug 功能,而不需要在 URL 中显示当前正在查看的记录的 ID。
例如,StackOverflow URL 如下所示:来自 Url Slug 的 MVC 动态视图。请注意,URL 包含记录的 ID。如果我为此页面添加书签,并且由于某种原因记录的 ID 发生了更改,那么我以后将无法查看该记录。
我想实现一个不依赖于记录 ID 的 URL slug 功能,因此它看起来像 https://stackoverflow.com/questions/mvc-dynamic-views-from-url-slug。
您能提供实现这一目标的步骤吗? 你是否会使用唱片的标题并将其slugify,保存该slug,并且不再更改它?
我正在寻找的示例 URL 是这样的 http://weblogs.asp.net/scottgu/archive/2010/10/04/jquery-templates-data-link-and-globalization-accepted -as-official-jquery-plugins.aspx(注意 URL 中没有 ID)
谢谢!
I would like to know how to implement a URL slug functionality in a website, without having the ID of the record currently being viewed show up in the URL.
For example, StackOverflow URLs look like this: MVC Dynamic Views From Url Slug. Notice the URL contains the ID of the record. If I were to bookmark this page, and for some reason the ID of the record changed, I would not be able to look at this record in the future.
I want to implement a URL slug functionality that doesn't depend on the ID of the record, so it will look something like https://stackoverflow.com/questions/mvc-dynamic-views-from-url-slug.
Can you please provide steps for achieving this?
Do you use the title of the record and sluggify it, save that slug, and never change it again?
A sample URL of what I am looking for is this http://weblogs.asp.net/scottgu/archive/2010/10/04/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins.aspx (Notice no ID in the URL)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这基本上就是你所做的。每种方法都有其优点:
您将需要一个数据库索引来提高 slug 查找的效率。幸运的是,Django 的 Slug 字段 自动创建数据库索引。
您可能仍然希望有一个数字主键,只是为了使数据库的其余部分更简单。
That’s basically what you do. Each method has its advantages:
You will need a database index to make slug lookup efficient. Fortunately, Django’s Slug field automatically creates a database index.
You probably still want to have a numeric primary key, simply to make the rest of your database simpler.
您是否正在寻找URL重写?
Are you looking for URL Rewriting?
归根结底,您需要一些独特的方法来识别每种资源。大多数情况下,它是一个整数,因为它比 GUID 对数据库更友好并且更人性化。如果您想使用任意字符串,这与整数 ID 的通用技术相同,但您必须小心,不要最终出现 2 个或更多记录共享相同的 slug。
At the end of the day, you need some unique method of identifying each resource. Most often that's an integer because it's database-friendly and more human-friendly than a GUID. If you want to use an arbitrary string instead, it's the same general technique as for an integer ID, but you have to be careful not to end up with 2 or more records sharing the same slug.