on branch: dev/ckeditor
Nowadays it is common practice to use a slug in the URL. The slug is easier for humans and search engines to read, making your site more accessible.
I have added the slug
attribute to the Project
model:
# projects/models.py
class Project(models.Model):
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250)
Don't forget to make the migrations:
python manage.py makemigrations
python manage.py migrate
In the admin panel you can configure ProjectAdmin
to automatically pre-populate the slug
attribute when adding a title
.
# projects/admin.py
@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ["title"]}
The view doesn't need to be changed.
# projects/views.py
class PortfolioDetailView(DetailView):
model = Project
template_name = "portfolio_details.html"
But we have to change the url pattern:
# projects/urls.py
urlpatterns = [
path("portfolio/<slug:slug>/", PortfolioDetailView.as_view(), name="portfolio-detail"),
]
And to call the view, we need to change the template where the view is called.
<!-- projects/templates/portfolio.html -->
<a href="{% url 'projects:portfolio-detail' slug=project.slug %}" title="{{ project.title }}">
<i class="bx bx-link"></i>
</a>