Django Slug (Slugify URL)
Simple slugify URL in Django.
Official docs here.
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
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
Register Model in 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
Step 6: Update Views
Update myapp/views.py. Add Function Based View method that render book.html.
Step 7: Setup URL
Create myapp/urls.py
Update 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.
Open http://127.0.0.1:8000/book/<slug>