Django 2.1如何使用自定义函数显示上下文

时间:2019-04-25 22:35:46

标签: django python-3.x django-templates django-views django-2.1

我有一个侧边栏,显示按评论数量排序的热门帖子。

在模板中使用{{ post.message_html|truncatechars:60|safe }}显示帖子内容。

要获取popular_posts,请使用context_processors.py:

def get_popular_posts(request):
    context = {}
    context["popular_posts"] = (
        Post.objects.all()
        .annotate(comments_count=Count("comments"))
        .order_by("-comments_count")[:3]
    )
    return context

如何通过post.message_html的某些自定义功能(相同)处理popular_posts?该功能应该写在哪里?

models.py:

class Post(models.Model):
    user = models.ForeignKey(User, related_name="posts", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now=True)
    message = models.TextField()
    message_html = models.TextField(editable=False)
    group = models.ForeignKey(
        Group, related_name="posts", null=True, blank=False, on_delete=models.CASCADE
    )

    def __str__(self):
        return self.message

    def save(self, *args, **kwargs):
        self.message_html = misaka.html(self.message)
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse(
            "posts:single", kwargs={"username": self.user.username, "pk": self.pk}
        )

    class Meta:
        ordering = ["-created_at"]
        unique_together = ["user", "message"]

这是我包含在其他模板中的.html模板:

<div class="left-bar">
  <h4 style="text-align: center;">POPULAR POSTS:</h4>
  <hr>

  {% for post in popular_posts %}
    <div class="jumbotron">
      <a href="{% url 'posts:single' username=post.user.username pk=post.pk %}">{{ post.message_html|truncatechars:60|safe }}</a>
        <hr>
        <span class="text-muted popular">
          <p>comments: {{ post.comments.count }}</p>
          <p>author: {{ post.user.username }}</p>
        </span>
    </div>
  {% endfor %}

</div>

1 个答案:

答案 0 :(得分:1)

为此,您可以创建自定义模板标签或过滤器。 docs

模板过滤器可以像{{ post.message_html|truncatechars:60|safe|process_message }}

一样使用
# /app/templatetags/utils.py

register = template.Library()

@register.filter(name='process_message')
def process_message(value):
    # do something
    return result  #return processed message