当我阅读Django代码时,我经常在模型中看到所谓的“slug”。我不太确定这是什么,但我知道它与url有关。这个鼻涕虫什么时候该怎么用?
(我已在本术语表中阅读了它的定义。)
鼻涕虫 某物的缩写标签,只包含字母、数字、 下划线或连字符。它们通常用于url中。例如, 在一个典型的博客入口URL中: https://www.djangoproject.com/weblog/2008/apr/12/spring/最后一位 (弹簧)是鼻涕虫。
当我阅读Django代码时,我经常在模型中看到所谓的“slug”。我不太确定这是什么,但我知道它与url有关。这个鼻涕虫什么时候该怎么用?
(我已在本术语表中阅读了它的定义。)
鼻涕虫 某物的缩写标签,只包含字母、数字、 下划线或连字符。它们通常用于url中。例如, 在一个典型的博客入口URL中: https://www.djangoproject.com/weblog/2008/apr/12/spring/最后一位 (弹簧)是鼻涕虫。
当前回答
请允许我提供一些历史背景:
术语“弹头”与铸造金属铅有关,在这种情况下,印刷字体是由金属铅制成的。然后,每张纸的字体工厂都会定期重新熔化并在新的模具中重新铸造,因为经过多次打印后,它们会磨损。像我这样的学徒在那里开始了他们的职业生涯,并一路走到顶峰(不再)。
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.
姜戈走出了劳伦斯杂志在堪萨斯州的办公室。那里可能还有一些印刷术语。A-django-enthusiast -&-friendly-old-slug-boy-from-France。
其他回答
简而言之,slug帮助摆脱那些丑陋的url与有效的url为例,在一个电子商务网站,而不是显示url为www.myecom.com/product/5432156,我可以显示它像www.myecom.com/product/iphone11与slug的帮助
Slug是针对特定内容的URL友好的短标签。它只包含字母,数字,下划线或连字符。鼻涕虫通常与各自的内容一起保存,并作为URL字符串传递。
Slug可以使用SlugField创建
Ex:
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)
你懒得做鼻涕虫过程吗?别担心,这个插件会帮你的。 django-autoslug
请允许我提供一些历史背景:
术语“弹头”与铸造金属铅有关,在这种情况下,印刷字体是由金属铅制成的。然后,每张纸的字体工厂都会定期重新熔化并在新的模具中重新铸造,因为经过多次打印后,它们会磨损。像我这样的学徒在那里开始了他们的职业生涯,并一路走到顶峰(不再)。
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.
姜戈走出了劳伦斯杂志在堪萨斯州的办公室。那里可能还有一些印刷术语。A-django-enthusiast -&-friendly-old-slug-boy-from-France。
它是URL的描述部分,使其更人性化的描述,但不一定是web服务器所要求的——在Django中什么是“slug”?这个slug是'in-django-what-is-a-slug',但是这个slug并不用于决定服务的页面(至少在这个站点上)。
Slug在Django中用于动态生成一个人类友好/可读的URL。例如,这个页面的当前URL是这样的: Django中的“slug”是什么?
注意URL是如何被实际的问题(在Django中什么是slug)所代替的