From 0a7a956ae057598f53020492ffd7dfa30274b3aa Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 18 Aug 2020 19:51:54 +0200 Subject: [PATCH] Add links to comments admin --- articles/models.py | 16 ++++++++++++++-- articles/templates/articles/article_detail.html | 6 +++--- articles/views/html.py | 3 +++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/articles/models.py b/articles/models.py index 936c374..ee8e85a 100644 --- a/articles/models.py +++ b/articles/models.py @@ -19,7 +19,16 @@ class ArticleManager(models.Manager): return super().get_queryset().filter(page__isnull=True) -class Article(models.Model): +class AdminUrlMixin: + def get_admin_url(self): + content_type = ContentType.objects.get_for_model(self.__class__) + return reverse( + "admin:%s_%s_change" % (content_type.app_label, content_type.model), + args=(self.id,), + ) + + +class Article(AdminUrlMixin, models.Model): DRAFT = "draft" PUBLISHED = "published" STATUS_CHOICES = [ @@ -92,7 +101,7 @@ class Page(Article): ordering = ["position", "-published_at"] -class Comment(models.Model): +class Comment(AdminUrlMixin, models.Model): username = models.CharField( max_length=255, help_text="Will be displayed with your comment." ) @@ -116,3 +125,6 @@ class Comment(models.Model): class Meta: ordering = ["created_at"] + + def get_absolute_url(self): + return self.article.get_absolute_url() + "#" + str(self.id) diff --git a/articles/templates/articles/article_detail.html b/articles/templates/articles/article_detail.html index 1d73932..9b0f2d5 100644 --- a/articles/templates/articles/article_detail.html +++ b/articles/templates/articles/article_detail.html @@ -14,12 +14,12 @@

Comments

{% for comment in comments %} -
+

{{ comment.content|linebreaksbr }} @@ -28,7 +28,7 @@ {% empty %} No reaction yet, write your own! {% endfor %} -

+ {% csrf_token %} {{ form.as_table }} diff --git a/articles/views/html.py b/articles/views/html.py index a1fc3cc..49497d4 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -74,6 +74,9 @@ class ArticleDetailView(FormMixin, generic.DetailView): return self.form_invalid(form) def form_invalid(self, form): + messages.error( + self.request, "Your comment couldn't be saved, see the form below." + ) return super().form_invalid(form) def form_valid(self, form):