Django, how create Slug Fields in Django,

Educations 3 Fri 08 Jul 2022
Django, how …

Django auto Slug 

SlugField is most important part of generating a valid URL, slug url  containing only letters, numbers, underscores or hyphens.

For example, my website is and I have create one post and I using posting title “  how create slug  field in django”


Here if I want  my url show with by post title  like” create slug field in django” then without slug fields not possible showing correct,  without slug field you have get this is url like “

It means without slug fields not possible url showing correct formate.


So django provide future slug fields option  and this option is auto create . here you look how create auto slug field.

1st   you have setup your .venv

2nd create your project django-admin startproject project   


3rd crate your application under project, here application name is slug

python startapp slug  

Python makemigrations

Python migrate

Python runserver

look like below windows.

Django welcome page

 4rd slug application add in your file

INSTALLED_APPS = [    ...    "articles",  # new]


5th now pip install django-autoslug

Pip install django-autoslug

6th also django-autoslug field add in

INSTALLED_APPS = [    ...    "articles",  # new    “django-autoslug”, # slug fields]



7th  now create

from django.db.models import CharField, Model

from autoslug import AutoSlugField

          class Post(models.Model):

                   title = models.CharField(max_length = 250)

          slug = AutoSlugField(populate_from='title')

                   text = models.TextField()


def __str__(self):

             return self.title



8th now fields in ADMIN.PY

from django.contrib import admin from .models import post class postAdmin(admin.ModelAdmin):    list_display = ("title", "text",), postAdmin)





 9th you now create   

from .models import post  def postview(request, slug):   


   return render (request, “yourhtmlfile.html”, {‘post:allpost})  

10th  now create file

from django.urls import path

from .models import post 

from .views import postview

urlpatterns = [       

      path("home", views. postview name="home"),  

      path("home/<slug>", views. postview name="postdetails"), 



Again do

Python makemigrations

Python migrate

Python runserver


After creating all function you can able to fetching data in your templates file. you can create accounting to your requirement  here I have giving you tips for slug fields adding only.


--Posted By : santosh

Back to Post

All Comments...

No comments yet !!!!


Online Store

Populer Store
Electronics Store
Services Store
Other Services