in Technology by
What is a “slug” in Django?

1 Answer

0 votes
by

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

Related questions

0 votes
    Django is written in which programming language? 1. PHP 2. Python 3. Java 4.. C++...
asked Jul 4, 2021 in Technology by JackTerrance
0 votes
    Django is based on which architecture? 1. MTV 2. MVC 3. MVTT 4. MVP...
asked Jul 4, 2021 in Technology by JackTerrance
0 votes
    Which file is responsible for the configurations of the Django applications? 1. settings.py 2. manage.py 3. wsgi.py 4. app.py...
asked Jul 4, 2021 in Technology by JackTerrance
0 votes
    Which of these databases are not by default supported by Django? 1. Postgres 2. Mysql 3. Sqlite 4. Mongodb...
asked Jul 4, 2021 in Technology by JackTerrance
0 votes
    does – why doesn’t Django?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
0 votes
    Explain Q objects in Django ORM?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    What are the ways to customize the functionality of the Django admin interface?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    How to obtain the SQL query from the queryset in Django?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    How to get a particular item in the Model in Django?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    What is the difference between Django OneToOneField and ForeignKey Field?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    Why is permanent redirection not a good option in Django?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
0 votes
    How to use file-based sessions in Django?...
asked Jul 2, 2021 in Technology by JackTerrance
0 votes
    How to filter items in the Model in Django?...
asked Jul 1, 2021 in Technology by JackTerrance
...