在Django中,“子弹”是什么?

问题:在Django中,“子弹”是什么?

当我阅读Django代码时,我经常在模型中看到所谓的“子弹”。我不太清楚这是什么,但是我知道它与URL有关。应该如何以及何时使用这种this子?

(我已经在本词汇表中阅读了其定义。)

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 in this glossary.)


回答 0

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

一个例子:

<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

由于网址中的空格无效,因此必须将其替换为%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是一个子弹:它是通过将所有字母缩进小写并用连字符替换空格来从标题创建的-

另请参见此网页的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.


回答 1

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

术语“弹头”与铸造金属铅,在这种情况下,外面的新闻字体被做事情。然后,每张纸的字体工厂都会定期重新熔化,并用新鲜的模具重新铸造,因为经过多次印刷后,它们已经磨损了。像我这样的学徒从那里开始了他们的职业生涯,并一路攀升到了顶峰(不再有)。

排版者必须以倒排的方式撰写文章文字,并以明智的方式堆叠主角。因此,在打印时,字母将直接在纸上。所有的印刷机都能像打印的报纸一样快地阅读报纸。因此,(如蜗牛)和慢故事(最后一个要修复的故事)在板凳上等待着很多,只能通过拳头字母来识别,大部分标题通常更易读。一些“热门”新闻在板凳上等待着,可能在最后组装和确定印刷之前进行最后一分钟的更正(晚上用纸)。

Django来自堪萨斯州《劳伦斯》杂志的办公室。也许还有一些印刷术语仍然存在。django爱好者和来自法国的友好的老-男孩。

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.


回答 2

“子弹”一词来自报纸生产的世界。

这是制作过程中故事的非正式名称。随着故事的发展,从节拍记者(假设这些故事甚至还存在吗?)一直到编辑器再到“印刷机”,这就是它所引用的名称,例如,“您是否已修复了’凯特和威廉的故事?”。

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

甚至Stack Overflow本身也可以使用GEB-ish (a)自引用进行此操作/programming/427102/what-is-a-slug-in-django/427201#427201,尽管您可以将其替换为blahblah,但仍然可以。

它可能甚至早于该日期,因为编剧在每个场景的开始处都有“条形线”,这基本上设置了该场景的背景(位置,时间等)。这非常相似,是随后内容的序言或序言。

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

尽管以下是纯粹的推测,但“鼻涕虫”的早期含义是使用伪造的硬币(必须以某种方式加以按压)。我可以设想将用法转换为印刷术语(因为必须使用原始字符压制嵌条),然后从“金属片”定义更改为“故事摘要”定义。从那里开始,从正确的打印到在线世界仅一步之遥。


(a)道格拉斯·霍夫施塔特Douglas Hofstadter )的作品《巴赫·戈德尔·埃舍尔》(Godel Escher,Bach),我(至少)认为这是伟大的现代知识分子作品之一。您还应该查看他的其他作品“ 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”.


回答 3

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

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

就像您在Stack Overflow上添加此问题的方式一样,并生成了一个动态页面,当您在地址栏中看到时,您会看到问题标题带有“-”代替空格。这正是田的工作。

您输入的标题是这样的->在Django中,“子弹”是什么?

将其存储到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.

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)


回答 4

这里

“ Slug”是报纸上的术语,但此处的含义是URL的最后一部分。例如,标题为“关于Django的一点”的帖子将自动变为“ bit-about-django”(当然,如果您不喜欢自动生成的子弹,则可以轻松地对其进行更改)。

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).


回答 5

它是URL的描述性部分,可以使它更具人工描述性,但不一定是Web服务器所必需的- 在Django中,“子弹”是什么?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)


回答 6

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

Slug可以使用SlugField创建

例如:

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

如果您想使用标题作为标题,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)

如果需要唯一性,请添加unique=True子弹字段。

例如,从前面的示例中:

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

您是否懒于进行处理?不用担心,此插件将为您提供帮助。 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


回答 7

也可以在django-admin上自动启动。在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', )}

回答 8

ug

某物的简短标签,仅包含字母,数字,下划线或连字符。它们通常在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.