Add slug
This commit is contained in:
parent
54d8d989be
commit
0fb2ff99bd
5 changed files with 35 additions and 5 deletions
|
@ -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"):
|
||||
|
|
22
articles/migrations/0005_article_slug.py
Normal file
22
articles/migrations/0005_article_slug.py
Normal file
|
@ -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,
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
<h1>{{ title }} list</h1>
|
||||
{% for article in articles %}
|
||||
<article>
|
||||
<h2><a href="{% url 'article-detail' pk=article.pk %}">{{ article.title }}</a></h2>
|
||||
<h2><a href="{% url 'article-detail' slug=article.slug %}">{{ article.title }}</a></h2>
|
||||
{% include "articles/metadata_snippet.html" %}
|
||||
<p>{{ article.get_abstract|safe }}</p>
|
||||
<p><a href="{% url 'article-detail' pk=article.pk %}">Read more</a></p>
|
||||
<p><a href="{% url 'article-detail' slug=article.slug %}">Read more</a></p>
|
||||
</article>
|
||||
{% empty %}
|
||||
<p>No article here. Come back later 🙂</p>
|
||||
|
|
|
@ -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("<int:pk>", html.ArticleDetailView.as_view(), name="article-detail"),
|
||||
path("<slug:slug>", html.ArticleDetailView.as_view(), name="article-detail"),
|
||||
path("feed/", feeds.CompleteFeed(), name="complete-feed"),
|
||||
]
|
||||
|
|
Reference in a new issue