Jonas Jonas - 1 year ago 108
Python Question

What is a "slug" in Django?

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 this slug-thing supposed to be used?

(I have read its definition in this glossary.)

Answer Source

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

Or, you could reference the title like so: 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:

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

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download