Search code examples
djangopython-3.xdjango-templatesdjango-viewsdjango-2.1

Django 2.1 how to use custom function to display context


I have a sidebar that displays popular posts ordered by amount of their comments.

Piece of post's content is displayed using {{ post.message_html|truncatechars:60|safe }} in template.

To get popular_posts I use 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

How can I have the post.message_html processed by some custom function (the same) for each of popular_posts? Where this function should be written?

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"]

and here is my .html template that I include in other template:

<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>

Solution

  • You can create a custom template tag or a filter for this purpose. docs.

    Template filter can be used like {{ 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