什么是“蛞蝓”? 在姜戈?

发布于 2024-07-11 16:28:01 字数 507 浏览 9 评论 0原文

当我阅读 Django 代码时,我经常在模型中看到所谓的“slug”。 我不太确定这是什么,但我知道它与 URL 有关。 这个蛞蝓状的东西应该如何以及何时使用?

我已在此术语表中阅读了其定义:

蛞蝓
某物的简短标签,仅包含字母、数字、 下划线或连字符。 它们通常用在 URL 中。 例如, 在典型的博客条目 URL 中:

https://www.djangoproject.com/weblog/2008/apr /12/spring/ 最后一点 (spring) 是蛞蝓。

When I read Django code I often see in models what is called a "slug". I am not quite sure what this is, but I do know it has something to do with URLs. How and when is this slug-thing supposed to be used?

I have read its definition below in this glossary:

Slug
A short label for something, containing only letters, numbers,
underscores or hyphens. They’re generally used in URLs. For example,
in a typical blog entry URL:

https://www.djangoproject.com/weblog/2008/apr/12/spring/ the last bit
(spring) is the slug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(14

流云如水 2024-07-18 16:28:01

“slug”是一种生成有效 URL 的方法,通常使用已获得的数据。 例如,slug 使用文章标题来生成 URL。 我建议在给定标题(或另一条数据)的情况下,通过函数生成 slug,而不是手动设置。

示例:

<title> The 46 Year Old Virgin </title>
<content> A silly comedy movie </content>
<slug> the-46-year-old-virgin </slug>

现在假设我们有一个 Django 模型,例如:

class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=1000)
    slug = models.SlugField(max_length=40)

您将如何使用 URL 和有意义的名称引用此对象? 例如,您可以使用 Article.id,这样 URL 将如下所示:

www.example.com/article/23

或者,您可能希望像这样引用标题:

www.example.com/article/The 46 Year Old Virgin

由于空格在 URL 中无效,因此必须将其替换为 %20,结果是:

www.example.com/article/The%2046%20Year%20Old%20Virgin

两次尝试都没有产生非常有意义、易于阅读的 URL。 这样更好:

www.example.com/article/the-46-year-old-virgin

在本例中,the-46-year-old-virgin 是一个 slug:它是通过将所有字母小写并用连字符替换空格来根据标题创建的 -

另请参阅该网页的 URL 以获取另一个示例。

A "slug" is a way of generating a valid URL, generally using data already obtained. For instance, a slug uses the title of an article to generate a URL. I advise to generate the slug by means of a function, given the title (or another piece of data), rather than setting it manually.

An example:

<title> The 46 Year Old Virgin </title>
<content> A silly comedy movie </content>
<slug> the-46-year-old-virgin </slug>

Now let's pretend that we have a Django model such as:

class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=1000)
    slug = models.SlugField(max_length=40)

How would you reference this object with a URL and with a meaningful name? You could for instance use Article.id so the URL would look like this:

www.example.com/article/23

Or, you might want to reference the title like this:

www.example.com/article/The 46 Year Old Virgin

Since spaces aren't valid in URLs, they must be replaced by %20, which results in:

www.example.com/article/The%2046%20Year%20Old%20Virgin

Both attempts are not resulting in very meaningful, easy-to-read URL. This is better:

www.example.com/article/the-46-year-old-virgin

In this example, the-46-year-old-virgin is a slug: it is created from the title by down-casing all letters, and replacing spaces by hyphens -.

Also see the URL of this very web page for another example.

幼儿园老大 2024-07-18 16:28:01

如果我可以提供一些历史背景:

术语“slug”与铸造金属(在本例中为铅)有关,印刷字体就是用铅制成的。 然后,每张纸的字体工厂都会定期重新熔化并用新模具重新铸造,因为经过多次印刷后,字体就会磨损。 像我这样的学徒在那里开始了他们的职业生涯,并一路走到了顶峰(不再是了)。

印刷术必须以向后的方式撰写文章的文本,并以明智的方式堆叠主角。 所以在打印时,字母会直接出现在纸上。 所有印刷体都能以与印刷报纸一样快的速度阅读镜像报纸。 因此,蛞蝓(像蜗牛)还有缓慢的故事(最后修复的)都在长凳上等待,仅通过它们的第一个字母来识别,大多数情况下整个标题通常更容易阅读。 一些“热门”新闻在板凳上等待着,可能在最后一刻进行修正(《晚报》),然后再进行最后的组装和最终印刷。

姜戈从堪萨斯州劳伦斯杂志的办公室里出来。 那里可能仍然存在一些印刷术语。 来自法国的阿姜戈爱好者和友好的老蛞蝓男孩。

If I may provide some historical context :

The term "slug" has to do with casting metal—lead, in this case—out of which the press fonts were made. Every paper then had its fonts factory regularly re-melted and recast in fresh molds, since after many prints they became worn out. Apprentices like me started their career there, and went all the way to the top (not anymore).

Typographs had to compose the text of an article in a backward manner with lead characters stacked in a wise. So at printing time the letters would be straight on the paper. All typographs could read the newspaper mirrored as fast as the printed one. Therefore the slugs, (like snails) also the slow stories (the last to be fixed) were many on the bench waiting, solely identified by their fist letters, mostly the whole title generally more readable. Some "hot" news were waiting there on the bench, for possible last minute correction, (Evening paper) before last assembly and definitive printing.

Django emerged from the offices of the Lawrence journal in Kansas. Where probably some printing jargon still lingers. A-django-enthusiast-&-friendly-old-slug-boy-from-France.

溇涏 2024-07-18 16:28:01

“slug”一词来自报纸生产领域。

这是在制作过程中给故事起的非正式名称。 当故事从节拍记者(假设这些甚至不再存在?)到编辑再到“印刷机”时,这就是它所引用的名称,例如“有你修正了‘凯特和威廉’故事中的那些错误吗?”

某些系统(例如 Django)使用 slug 作为 URL 的一部分来定位故事,例如 www.mysite.com/archives/kate-and-william

甚至 Stack Overflow 本身也这样做,通过 GEB-ish(a) 自引用 https://stackoverflow.com/questions/427102/what-is-a-slug-in- django/427201#427201,尽管你可以用 blahblah 替换 slug,但它仍然会发现没问题。

它甚至可以追溯到更早的时期,因为剧本在每个场景的开头都有“台词”,这基本上设置了该场景的背景(地点、时间等)。 它非常相似,因为它是以下内容的概要或序言。

在 Linotype 机器上,字条是由单个字母形式创建的单行金属片。 通过为整行制作一个 slug,这大大改进了旧的逐字符合成。

尽管以下内容纯粹是猜测,但 slug 的早期含义是假币(必须以某种方式压制)。 我可以设想将用法转换为印刷术语(因为必须使用原始字符来按下弹头),并从那里从“金属片”定义更改为“故事摘要”定义。 从那里,从正确的印刷到在线世界只有很短的一步。


(a)“哥德尔·埃舍尔,巴赫”,作者:道格拉斯·霍夫施塔特 ,我(至少)认为这是最伟大的现代知识分子著作之一。 您还应该看看他的另一部作品“Metamagical Themas”。

The term 'slug' comes from the world of newspaper production.

It's an informal name given to a story during the production process. As the story winds its path from the beat reporter (assuming these even exist any more?) through to editor through to the "printing presses", this is the name it is referenced by, e.g., "Have you fixed those errors in the 'kate-and-william' story?".

Some systems (such as Django) use the slug as part of the URL to locate the story, an example being www.mysite.com/archives/kate-and-william.

Even Stack Overflow itself does this, with the GEB-ish(a) self-referential https://stackoverflow.com/questions/427102/what-is-a-slug-in-django/427201#427201, although you can replace the slug with blahblah and it will still find it okay.

It may even date back earlier than that, since screenplays had "slug lines" at the start of each scene, which basically sets the background for that scene (where, when, and so on). It's very similar in that it's a precis or preamble of what follows.

On a Linotype machine, a slug was a single line piece of metal which was created from the individual letter forms. By making a single slug for the whole line, this greatly improved on the old character-by-character compositing.

Although the following is pure conjecture, an early meaning of slug was for a counterfeit coin (which would have to be pressed somehow). I could envisage that usage being transformed to the printing term (since the slug had to be pressed using the original characters) and from there, changing from the 'piece of metal' definition to the 'story summary' definition. From there, it's a short step from proper printing to the online world.


(a) "Godel Escher, Bach", by one Douglas Hofstadter, which I (at least) consider one of the great modern intellectual works. You should also check out his other work, "Metamagical Themas".

冷…雨湿花 2024-07-18 16:28:01

Slug 是一个报纸术语。 slug 是某事物的简短标签,仅包含字母、数字、下划线或连字符。 它们通常用在 URL 中。 (如 Django 文档中所示)

Django 中的 slug 字段用于存储和生成有效的 URL动态创建的网页。

就像您在 Stack Overflow 上添加此问题并生成动态页面一样,当您在地址栏中看到时,您将看到问题标题用“-”代替空格。 这正是蛞蝓场的工作。

在此处输入图像描述

您输入的标题是这样的 -> Django 中的“slug”是什么?

将其存储到 slug 字段后,它会变成“what-is-a-slug-in-django”(请参阅​​本页的 URL)

Slug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs. (as in Django docs)

A slug field in Django is used to store and generate valid URLs for your dynamically created web pages.

Just like the way you added this question on Stack Overflow and a dynamic page was generated and when you see in the address bar you will see your question title with "-" in place of the spaces. That's exactly the job of a slug field.

Enter image description here

The title entered by you was something like this -> What is a “slug” in Django?

On storing it into a slug field it becomes "what-is-a-slug-in-django" (see URL of this page)

风柔一江水 2024-07-18 16:28:01

Slug 是针对特定内容的 URL 友好短标签。 它仅包含字母、数字、下划线或连字符。 Slug 通常与相应的内容一起保存,并作为 URL 字符串传递。

Slug 可以使用 SlugField 创建

例如:

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)

如果你想使用 title 作为 slug,django 有一个简单的函数,名为 slugify

from django.template.defaultfilters import slugify

class Article(models.Model):
    title = models.CharField(max_length=100)

    def slug(self):
        return slugify(self.title)

如果需要唯一性,请在 slug 字段中添加 unique=True

例如,从前面的示例来看:

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100, unique=True)

您是否懒惰进行 slug 过程? 别担心,这个插件会帮助你。
django-autoslug

Slug is a URL friendly short label for specific content. It only contain Letters, Numbers, Underscores or Hyphens. Slugs are commonly save with the respective content and it pass as a URL string.

Slug can create using SlugField

Ex:

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)

If you want to use title as slug, django has a simple function called slugify

from django.template.defaultfilters import slugify

class Article(models.Model):
    title = models.CharField(max_length=100)

    def slug(self):
        return slugify(self.title)

If it needs uniqueness, add unique=True in slug field.

for instance, from the previous example:

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100, unique=True)

Are you lazy to do slug process ? don't worry, this plugin will help you.
django-autoslug

不知在何时 2024-07-18 16:28:01

来自此处

“Slug”是一个报纸术语,但是什么
这意味着这是最后一点
网址。 例如,一篇带有
标题,“关于 Django 的一点”
成为“关于 django 的一点”
自动(当然,您可以
如果您不喜欢,可以轻松更改
自动生成的 slug)。

From here.

“Slug” is a newspaper term, but what
it means here is the final bit of the
URL. For example, a post with the
title, “A bit about Django” would
become, “bit-about-django”
automatically (you can, of course,
change it easily if you don’t like the
auto-generated slug).

未蓝澄海的烟 2024-07-18 16:28:01

它是 URL 的描述性部分,旨在使其更具人性化描述性,但 Web 服务器不一定需要 - 在 Django 中的“slug”是什么? 该 slug 是“in-django-what-is-a-slug”,但该 slug 不用于确定 提供的页面(至少在本网站上)

It's a descriptive part of the URL that is there to make it more human descriptive, but without necessarily being required by the web server - in What is a "slug" in Django? the slug is 'in-django-what-is-a-slug', but the slug is not used to determine the page served (on this site at least)

拥有 2024-07-18 16:28:01

还可以在 django-admin 处自动 slug。 在 ModelAdmin 中添加:

prepopulated_fields = {'slug': ('title', )}

如下所示:

class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug')
    search_fields = ('content', )

    prepopulated_fields = {'slug': ('title', )}

Also auto slug at django-admin. Added at ModelAdmin:

prepopulated_fields = {'slug': ('title', )}

As here:

class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug')
    search_fields = ('content', )

    prepopulated_fields = {'slug': ('title', )}
勿忘初心 2024-07-18 16:28:01

slug 是 URL 的一部分,它以易于阅读的形式标识网站上的特定页面。

例如,/building-your-1st-django-site

仅 Slug 包含:

  • 字母:az,AZ
  • 数字:0-9
  • 下划线:_
  • 连字符:-

A slug is the part of a URL which identifies a particular page on a website in an easy to read form.

For example, /building-your-1st-django-site.

Slug only Contains:

  • Letters : a-z,A-Z
  • Numbers : 0-9
  • Underscores : _
  • Hyphens : -
七分※倦醒 2024-07-18 16:28:01

slug

某事物的简短标签,仅包含字母、数字、下划线或连字符。 它们通常用在 URL 中。 例如,在典型的博客条目 URL 中:

https://www.djangoproject .com/weblog/2008/apr/12/spring/
最后一点(弹簧)是鼻涕虫。

slug

A short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs. For example, in a typical blog entry URL:

https://www.djangoproject.com/weblog/2008/apr/12/spring/
the last bit (spring) is the slug.

我家小可爱 2024-07-18 16:28:01

它是一种生成有效 URL 的方法,通常使用已经获得的数据。 例如,使用文章标题生成 URL。

It is a way of generating a valid URL, generally using data already obtained. For instance, using the title of an article to generate a URL.

乙白 2024-07-18 16:28:01

Slug 在 Django 中用于动态生成人类友好/可读的 URL。 例如,该页面的当前 URL 为:
Django 中的“slug”是什么?

注意 URL 如何与实际问题进行 slugify(Django 中的 slug 是什么)

Slug is used in Django to dynamically generate a human-friendly/readable URL. For example, the current URL of this page says:
What is a "slug" in Django?.

Notice how the URL was slugify with the actual question(what is a slug in Django)

不念旧人 2024-07-18 16:28:01

简而言之,slug 有助于摆脱那些带有有效网址的丑陋网址,例如在电子商务网站中,而不是将网址显示为 www.myecom.com/product/5432156 我可以像 那样显示它>www.myecom.com/product/iphone11 在 slug 的帮助下

In short slug help get rid of those ugly looking urls with valid-urls for examples in an ecommerec site instead of showing the url as www.myecom.com/product/5432156 i can show it like www.myecom.com/product/iphone11 with the help of slug

烟酒忠诚 2024-07-18 16:28:01

除了其他答案之外,Google 搜索中心还表示 <如下图:

考虑使用连字符来分隔网址中的单词,因为它会有所帮助
用户和搜索引擎更容易识别 URL 中的概念。 我们
建议您在代码中使用连字符 (-) 而不是下划线 (_)
网址。

因此,您应该使用 - 来分隔 slug 的单词,如下所示:

http://example.com/blog/my-1st-blog
                      #   ↑   ↑ Use hyphens

而不是如下所示的 _

http://example.com/blog/my_1st_blog
                      #   ↑   ↑ Don't use underscores

另外, - 用于分隔 slug 的问题标题 What is a "slug" in Django? ,如下所示:

https://stackoverflow.com/questions/427102/what-is-a-slug-in-django
                            # Hyphens are used ↑  ↑ ↑    ↑  ↑

In addition to other answers, Google Search Central says for SEO as shown below:

Consider using hyphens to separate words in your URLs, as it helps
users and search engines identify concepts in the URL more easily. We
recommend that you use hyphens (-) instead of underscores (_) in your
URLs.

So, you should use - to separate the words of slug as shown below:

http://example.com/blog/my-1st-blog
                      #   ↑   ↑ Use hyphens

Instead of _ as shown below:

http://example.com/blog/my_1st_blog
                      #   ↑   ↑ Don't use underscores

In addition, - is used to separate your question title What is a "slug" in Django? of slug as shown below:

https://stackoverflow.com/questions/427102/what-is-a-slug-in-django
                            # Hyphens are used ↑  ↑ ↑    ↑  ↑
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文