在网站中实现 URL slug 功能

发布于 2024-09-25 22:23:38 字数 899 浏览 3 评论 0原文

我想知道如何在网站中实现 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 技术交流群。

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

发布评论

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

评论(3

醉城メ夜风 2024-10-02 22:23:38

这基本上就是你所做的。每种方法都有其优点:

  • 在 Stack Overflow 上,由于问题的标题可以更改,因此使用 ID 永久链接到问题,标题不正确的 URL 将简单地重定向。
  • 使用 slug 作为永久标识符意味着 slug 永远不会改变。更改标题是可能的,但这可能看起来很混乱,并且您最好重定向该标题。此外,这些 slugs 要么需要全局唯一,要么您需要找到一种方法来强制每年/月/日或其他任何方式的唯一性。

您将需要一个数据库索引来提高 slug 查找的效率。幸运的是,Django 的 Slug 字段 自动创建数据库索引。

您可能仍然希望有一个数字主键,只是为了使数据库的其余部分更简单。

That’s basically what you do. Each method has its advantages:

  • On Stack Overflow, since the title of a question can be changed, the ID is used to permanently link to the question, and URLs with an incorrect title will simply redirect.
  • Using the slug as the permanent identifier means the slug can never change. Changing the title will be possible, but it may seem confusing, and you might be better off redirecting the slug. Also, the slugs either need to be globally unique, or you need to figure out a way to enforce uniqueness for each year/month/day or whatever.

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.

不羁少年 2024-10-02 22:23:38

您是否正在寻找URL重写

Are you looking for URL Rewriting?

两仪 2024-10-02 22:23:38

归根结底,您需要一些独特的方法来识别每种资源。大多数情况下,它是一个整数,因为它比 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.

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