all that is technology

Automatically generating slug url in django


Auto generating a random clean URL (slug URL) for django web application. In URL the last part is auto generated random string.

We need to first install the following package

$ sudo pip install django-autoslug

then import the package

from autoslug.fields import AutoSlugField

In the model class add a field

url_ref = AutoSlugField(slugify=lambda instance: ''.join(random.sample(string.uppercase + string.lowercase + string.digits, 5)), unique=True)

Hope this helps.