Add links to comments admin
This commit is contained in:
parent
261120275d
commit
0a7a956ae0
3 changed files with 20 additions and 5 deletions
|
@ -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)
|
||||
|
|
|
@ -14,12 +14,12 @@
|
|||
<section class="comments">
|
||||
<h2>Comments</h2>
|
||||
{% for comment in comments %}
|
||||
<article class="comment">
|
||||
<article id="{{ comment.id }}" class="comment">
|
||||
<p class="metadata">
|
||||
<span class="username">{{ comment.username }}</span> |
|
||||
<time datetime="{{ comment.created_at|date:'c' }}">
|
||||
{{ comment.created_at|date:"DATETIME_FORMAT" }}
|
||||
</time>
|
||||
</time> {% include "articles/admin_link_snippet.html" with article=comment %}
|
||||
</p>
|
||||
<p class="content">
|
||||
{{ comment.content|linebreaksbr }}
|
||||
|
@ -28,7 +28,7 @@
|
|||
{% empty %}
|
||||
No reaction yet, write your own!
|
||||
{% endfor %}
|
||||
<form action="{% url 'create-comment' slug=article.slug %}" method="post">
|
||||
<form id="comment-form" action="{% url 'create-comment' slug=article.slug %}" method="post">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
{{ form.as_table }}
|
||||
|
|
|
@ -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):
|
||||
|
|
Reference in a new issue