What is a slug in Django? 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:
1 2 3 | <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:
1 2 3 4 | class Article(models.Model): title = models.CharField(max_length=100) content = models.TextField(max_length=1000) slug = models.SlugField(max_length=40) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.