什么是“蛞蝓”? 在姜戈?
当我阅读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
“slug”是一种生成有效 URL 的方法,通常使用已获得的数据。 例如,slug 使用文章标题来生成 URL。 我建议在给定标题(或另一条数据)的情况下,通过函数生成 slug,而不是手动设置。
示例:
现在假设我们有一个 Django 模型,例如:
您将如何使用 URL 和有意义的名称引用此对象? 例如,您可以使用 Article.id,这样 URL 将如下所示:
或者,您可能希望像这样引用标题:
由于空格在 URL 中无效,因此必须将其替换为
%20
,结果是:两次尝试都没有产生非常有意义、易于阅读的 URL。 这样更好:
在本例中,
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:
Now let's pretend that we have a Django model such as:
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:
Or, you might want to reference the title like this:
Since spaces aren't valid in URLs, they must be replaced by
%20
, which results in:Both attempts are not resulting in very meaningful, easy-to-read URL. This is better:
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.
如果我可以提供一些历史背景:
术语“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.
“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 withblahblah
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".
Django 中的 slug 字段用于存储和生成有效的 URL动态创建的网页。
就像您在 Stack Overflow 上添加此问题并生成动态页面一样,当您在地址栏中看到时,您将看到问题标题用“-”代替空格。 这正是蛞蝓场的工作。
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.
Slug 是针对特定内容的 URL 友好短标签。 它仅包含字母、数字、下划线或连字符。 Slug 通常与相应的内容一起保存,并作为 URL 字符串传递。
Slug 可以使用 SlugField 创建
例如:
如果你想使用 title 作为 slug,django 有一个简单的函数,名为
slugify
如果需要唯一性,请在 slug 字段中添加
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:
If you want to use title as slug, django has a simple function called
slugify
If it needs uniqueness, add
unique=True
in slug field.for instance, from the previous example:
Are you lazy to do slug process ? don't worry, this plugin will help you.
django-autoslug
来自此处。
From here.
它是 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)
还可以在 django-admin 处自动 slug。 在 ModelAdmin 中添加:
如下所示:
Also auto slug at django-admin. Added at ModelAdmin:
As here:
slug 是 URL 的一部分,它以易于阅读的形式标识网站上的特定页面。
例如,
/building-your-1st-django-site
。仅 Slug 包含:
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:
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.
它是一种生成有效 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.
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)
简而言之,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 likewww.myecom.com/product/iphone11
with the help of slug除了其他答案之外,Google 搜索中心还表示 <如下图:
因此,您应该使用
-
来分隔slug
的单词,如下所示:而不是如下所示的
_
:另外,
-
用于分隔slug
的问题标题What is a "slug" in Django?
,如下所示:In addition to other answers, Google Search Central says for SEO as shown below:
So, you should use
-
to separate the words ofslug
as shown below:Instead of
_
as shown below:In addition,
-
is used to separate your question titleWhat is a "slug" in Django?
ofslug
as shown below: