What is 'SLUG' in Django?

Learn about the 'SLUG' in Django, how to create a SLUG?
Submitted by Apurva Mathur, on May 17, 2022

In Django, a "SLUG" is something that we write in our Url (Uniform Resource Locator) or we can say that it is something that will make it easy for the users on the internet to get through your page effortlessly. In simpler words, it helps a page to make it comfortably accessible on the internet. A "SLUG" contains some keywords which are related to your page i.e. it conveys to users what the post is all about.

"SLUG" is a useful concept as it assists SEO (search engine optimization, basically this concept gets pages to rank higher in search engines such as Google, Yahoo, Bing, etc.) to search in a way that may increase the traffic to a website. It is one of the indicators Google uses to determine what are the contents of the page.

Example:

Django | Slug (1)

This is the snap shot of a webpage which contains the information about seasons. As we can see it in the URL the season keyword is used by the admin and now Google uses this keyword to provide the users what are they searching for. This "SLUG" part is mainly for the search engines which search these keywords and then provide the result if those keywords are there in your URL.

So, let's see now how to create a "SLUG" in Django,

Step 1: Foremost step is to simply create a project on Django by typing django-admin startproject medassist on the terminal (you can write on CMD also, here I am using Pycharm).

Here, medassist is the name of the project you can keep anything according to your preference.

Django | Slug (2)

Step 2: Now create a template name folder under your main project (this template folder will contain all the HTML files). In this template make the HTML file that you wanted to request on your webpage. I have made my HTML file by "project_format" name which contains a simple article.

Django | Slug (3)

Step 3: Now simply just make view of this which will receive our web request and return a web response.

Here I have made my view in diplay.py file which will be under your main project file (where yours settings and other configurations files are there).

Django | Slug (4)

This code contains a simple function called "Interface" (you can give the name according to your preference) this function will request our html page and will take to that page.

Step 4: Here, you will create an URL where you will write some keywords about your content of the page which is called "SLUG".

To create the URL you will simply import the file and the function (in which you've your function to call the html page, here that file name is display.py) in url.py which will be in our main project folder, and after importing this just make the URL.

Django | Slug (5)

Do remember, display is our file name where are function is made and Interface is our function name is that file.

Step 5: Run this project on pycharm terminal by typing python manage.py runserver.

Final output:

Django | Slug (6)


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.