What is a “slug” in Django?

前端 未结 10 1782
感情败类
感情败类 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: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)

提交回复
热议问题