Add admin link to article

This commit is contained in:
Gabriel Augendre 2020-08-16 19:23:38 +02:00
parent bd55cc0e33
commit 9a50fa408f
4 changed files with 19 additions and 2 deletions

View file

@ -2,7 +2,9 @@ import re
import markdown
from django.contrib.auth.models import AbstractUser
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.urls import reverse
from django.utils import timezone
@ -32,6 +34,16 @@ class Article(models.Model):
def __str__(self):
return self.title
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,),
)
def get_absolute_url(self):
return reverse("article-detail", kwargs={"pk": self.pk})
def get_abstract(self):
html = self.get_formatted_content()
return html.split("<!--more-->")[0]

View file

@ -3,7 +3,7 @@
{% block content %}
<article>
<h1>{{ article.title }}{% if article.status != article.PUBLISHED %} ({{ article.status }}){% endif %}</h1>
{% include "articles/date_snippet.html" %}
{% include "articles/metadata_snippet.html" %}
<div>
{{ article.get_formatted_content|safe }}
</div>

View file

@ -5,7 +5,7 @@
{% for article in articles %}
<article>
<h2><a href="{% url 'article-detail' pk=article.pk %}">{{ article.title }}</a></h2>
{% include "articles/date_snippet.html" %}
{% include "articles/metadata_snippet.html" %}
<p>{{ article.get_abstract|safe }}</p>
<p><a href="{% url 'article-detail' pk=article.pk %}">Read more</a></p>
</article>

View file

@ -1,3 +1,8 @@
{% if user.is_authenticated %}
<p>
<a href="{{ article.get_admin_url }}">Admin</a>
</p>
{% endif %}
<p class="date">
{% if article.published_at %}
{{ article.published_at|date }}