Django Slug (Slugify URL)

Adi Ramadhan
2 min readMay 28, 2022

--

Simple slugify URL in Django.
Official docs here.

Slugify URL

Step 1: Preparation, Create Django Project, Initial Migration
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.2
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate

Step 2: Create Django Apps
Create apps: python manage.py startapp myapp
Add myapp to INSTALLED_APPS in myproject/settings.py

INSTALLED_APPS in myproject/settings.py

Step 3: Setup Templates
Create folder templates in myapp

Add template directory in TEMPLATES parameter in myproject/settings.py

Path(BASE_DIR , 'myapp', 'templates'),

Step 4: Add Model, Register Model in Django Admin, Makemigrations and Migrate
Add Model in myapp/models.py

myapp/models.py

Register Model in myapp/admin.py

myapp/admin.py

Make migrations: python manage.py makemigrations
Migrate: python manage.py migrate

Step 5: Create HTML Files in myapp/templates Folder
Create myapp/templates/book.html

myapp/templates/book.html

Step 6: Update Views
Update myapp/views.py. Add Function Based View method that render book.html.

myapp/views.py

Step 7: Setup URL
Create myapp/urls.py

myapp/urls.py

Update myproject/urls.py

myproject/urls.py

Step 8: Create Superuser
Create superuser: python manage.py createsuperuser
Type username, email, password and retype password

Step 9: Run Server and Testing
Run Server: python manage.py runserver
Testing: http://127.0.0.1:8000/admin
Try login, add some books.

Add book with prepopulated_fields for slug

Open http://127.0.0.1:8000/book/<slug>

--

--

Adi Ramadhan
Adi Ramadhan

No responses yet