What is a “slug” in Django?

前端 未结 10 1693
感情败类
感情败类 2020-11-22 14:59

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 th

相关标签:
10条回答
  • 2020-11-22 15:20

    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.

    0 讨论(0)
  • 2020-11-22 15:21

    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

    0 讨论(0)
  • 2020-11-22 15:25

    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)

    0 讨论(0)
  • 2020-11-22 15:28

    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.

    0 讨论(0)
提交回复
热议问题