views:

5114

answers:

5

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 URL:s. How and when is this slug-thing supposed to be used?

(I have read it's definition in this glossary)

+6  A: 

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

BobbyShaftoe
+8  A: 

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 http://stackoverflow.com/questions/427102/in-django-what-is-a-slug 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)

Cebjyre
+29  A: 

It's a way of generating a valid URL, generally using data already obtained. For instance, using the title of an article to generate a URL. I'd advise to generate the slug, using a function, given a title (or other piece of data), rather than setting it manually.

An example:

<title> The 46 Year Old Virgin </title>
<content> LOL!!! What a loser heh </content>
<slug> the-46-year-old-virgin </slug>

Now let's pretend that we have a Django model such as:

class Article():
    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, with a meaningful name? You could use Article.id so the URL would look like this:

www.example.com/Article/23

Or, you could reference the title like so:

www.example.com/article/The 46 Year Old Virgin

Problem is, spaces aren't valid in URLs, they need to be replaced by %20 which is ugly, making it the following:

www.example.com/article/The%2046%20Year%20Old%20Virgin

That's not solving our meaningful URL. Wouldn't this be better:

www.example.com/article/the-46-year-old-virgin

That's a slug. 'the-46-year-old-virgin'. All letters are downcased and spaces are replaced by hyphens '-'. See the URL of this very webpage for an example!

Josh Smeaton
Good description, but to add: SlugField doesn't ensure uniqueness out of the box, so if using it by default, this is a better URL:www.example.com/article/the-40-year-old-virgin/23/Where '23' is the article.id, and is what's actually used to do the query (also faster than querying on the slug).
Daniel
I find www.example.com/article/23/the-40-year-old-virgin easier to read.
MiniQuark
agreed MiniQuark
Josh Smeaton
You can also add a unique constraint to the slug.
Michael Warkentin
If you serve content based on the id, decide whether to check that the slug matches the content (and return 404 if it doesn't) or ignore it completely. I don't think there's a general rule for what's best, SEO-wise.SO will still display this page if you enter http://stackoverflow.com/questions/427102/in-django-what-the-hell-is-a-slug in your browser.
TomA
http://stackoverflow.com/questions/427102/your-mom-is-fat
Mark
+7  A: 

As a bit of history, the term 'slug' comes from the world of newspaper editing.

It's the informal name given to a story during the production process. As the story winds its torturous path from beat reporter 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 'russia-cuts-europe-gas' story?".

Django uses it as part of the URL to locate the story, an example being www.mysite.com/archives/russia-cuts-europe-gas.

paxdiablo
+7  A: 

May I be complete to this, The term "slug" has to do with casting metal, lead, in this case, out of which the press fonts where made of. Every Paper then, had it's fonts factory, regularily, re-melted and recasted in fresh molds.Because after many prints they where worned out. Apprentice like me started their career there,and all the way to the top. (Not anymore) Typographs had to compose the text of the article in a backward manner with lead caracters stacked in a wise. So at printing time le letters would be straight on the paper. All typhographs could read the newspaper mirrored has fast has the printed one.Therefore the slugs,(like snails) also the slow stories(the lasts to be fixed) were many on the bench waiting, solely identified by their fist letters, mostly le whole title generaly 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-enthousiast-&-friendly-old-slug-boy-from-France.