From 0fb2ff99bd175865bda581131b2bb5d9ef78aba7 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 16 Aug 2020 19:45:38 +0200 Subject: [PATCH] Add slug --- articles/admin.py | 3 ++- articles/migrations/0005_article_slug.py | 22 +++++++++++++++++++ articles/models.py | 9 +++++++- articles/templates/articles/article_list.html | 4 ++-- blog/urls.py | 2 +- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 articles/migrations/0005_article_slug.py diff --git a/articles/admin.py b/articles/admin.py index 31f52c1..dca6518 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -27,7 +27,7 @@ class ArticleAdmin(admin.ModelAdmin): "Metadata", { "fields": ( - "title", + ("title", "slug"), ("author", "status"), ("published_at", "created_at", "updated_at"), "views_count", @@ -42,6 +42,7 @@ class ArticleAdmin(admin.ModelAdmin): "widget": forms.Textarea(attrs={"cols": "100", "rows": "50"}) }, } + prepopulated_fields = {"slug": ("title",)} def publish(self, request, queryset): if not request.user.has_perm("articles.change_article"): diff --git a/articles/migrations/0005_article_slug.py b/articles/migrations/0005_article_slug.py new file mode 100644 index 0000000..c567cc5 --- /dev/null +++ b/articles/migrations/0005_article_slug.py @@ -0,0 +1,22 @@ +# Generated by Django 3.1 on 2020-08-16 17:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("articles", "0004_article_views_count"), + ] + + operations = [ + migrations.AddField( + model_name="article", + name="slug", + field=models.SlugField( + default="designing-and-3d-printing-a-wall-mount-for-my-isp-modem", + unique=True, + ), + preserve_default=False, + ), + ] diff --git a/articles/models.py b/articles/models.py index 8dcf576..a4d9f78 100644 --- a/articles/models.py +++ b/articles/models.py @@ -4,6 +4,7 @@ import markdown from django.contrib.auth.models import AbstractUser from django.contrib.contenttypes.models import ContentType from django.db import models +from django.template.defaultfilters import slugify from django.urls import reverse from django.utils import timezone @@ -27,6 +28,7 @@ class Article(models.Model): updated_at = models.DateTimeField(auto_now=True) author = models.ForeignKey(User, on_delete=models.PROTECT) views_count = models.IntegerField(default=0) + slug = models.SlugField(unique=True) class Meta: ordering = ["-published_at"] @@ -42,7 +44,7 @@ class Article(models.Model): ) def get_absolute_url(self): - return reverse("article-detail", kwargs={"pk": self.pk}) + return reverse("article-detail", kwargs={"slug": self.slug}) def get_abstract(self): html = self.get_formatted_content() @@ -64,3 +66,8 @@ class Article(models.Model): self.published_at = None self.status = self.DRAFT self.save() + + def save(self, *args, **kwargs): # new + if not self.slug: + self.slug = slugify(self.title) + return super().save(*args, **kwargs) diff --git a/articles/templates/articles/article_list.html b/articles/templates/articles/article_list.html index 221b55b..6745274 100644 --- a/articles/templates/articles/article_list.html +++ b/articles/templates/articles/article_list.html @@ -4,10 +4,10 @@

{{ title }} list

{% for article in articles %}
-

{{ article.title }}

+

{{ article.title }}

{% include "articles/metadata_snippet.html" %}

{{ article.get_abstract|safe }}

-

Read more

+

Read more

{% empty %}

No article here. Come back later 🙂

diff --git a/blog/urls.py b/blog/urls.py index 354d4b0..2ace0a2 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -22,6 +22,6 @@ urlpatterns = [ path("admin/", admin.site.urls), path("", html.ArticlesListView.as_view(), name="articles-list"), path("drafts/", html.DraftsListView.as_view(), name="drafts-list"), - path("", html.ArticleDetailView.as_view(), name="article-detail"), + path("", html.ArticleDetailView.as_view(), name="article-detail"), path("feed/", feeds.CompleteFeed(), name="complete-feed"), ]