What is a “slug” in Django?

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

    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.

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

    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)

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

    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 like www.myecom.com/product/iphone11 with the help of slug

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

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

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

    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', )}
    
    0 讨论(0)
  • 2020-11-22 15:19

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

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