blog preview

So I’m looking around for how to do this but haven’t found the answer yet. I have a blog model that includes html-formatted content using tinymce. I want to display the first maybe 300 characters and an image of that post, and then cut it off with a read more that redirects to the actual blog post. But all I know how to do is to display the entire post. Suggestions? ideas on what features to use? Maybe it’s javascript thing?
class Posts(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200,unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, default=get_ashley)
    updated_on = models.DateTimeField(auto_now=True)
    content = HTMLField()
    created_on = models.DateTimeField(auto_now_add=True)
    status = models.IntegerField(choices=STATUS, default=0)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return self.title
def admin_edits(request):
    context = {}

    # retrieve most recent blog
    recent_post = Posts.objects.filter(status=1).order_by('-created_on')[:1]
    context['recent_post'] = recent_post

    # retrieve upcoming events
    upcoming_events = Events.objects.order_by('event_date')[:3]
    context['upcoming_events'] = upcoming_events

    # retrieve most recent art
    recent_art = Art.objects.order_by('uploaded_on')[:1]
    context['recent_art'] = recent_art

    # retrive new users
    new_users = User.objects.order_by('date_joined')[:5]
    context['new_users'] = new_users

    return render(request, 'editors/admin_edits.html', context)

<section id="blog">
                <div id="recent_blog">
                {% if recent_blog %}
                    <h2>{{ recent_blog[0].title }}</h2>
                    <p> <!--preview would go here--></p>
                {% else %}
                    <p>There are no blog posts to display</p>
                {% endif %}
                <a href="{% url 'editors:blog_editor' %}">Go to full blog editor</a>
            <section id="calendar">
                <div id="upcoming_events">
                {% if upcoming_events %}
                    {% for event in upcoming_events %}
                        <p><strong>Event:</strong> {{ event.event_name }}, <strong>Date:</strong> {{ event.event_date }}</p>
                        <p>{{ event.event_description }}
                    {% endfor %}
                {% else %}
                    <p>There are no upcoming events</p>
                {% endif %}
                <a href="{% url 'editors:calendar_events' %}">Go to full calendar</a>
            <section id="gallery">
                <div id="recent_art">
                    {% if recent_art %}
                        {% for art in recent_art %}

                        {% endfor %}
                    {% else %}
                        <p>No art projects have been uploaded</p>
                    {% endif %}
                <a href="{% url 'editors:gallery_editor' %}">
            <section id="users">
                <div id="new_users">
                {% if new_users %}
                    {% for user in new_users %}
                            <li><strong>Username:</strong> {{ user.username }}</li>
                            <li><strong>Name:</strong> {{ user.first_name }} {{ user.last_name }}
                        <br />
                    {% endfor %}
                {% else %}
                    <p>There are no users on this system.</p>
                {% endif %}
                <a href="{% url 'editors:user_control' %}">

for context, this is the home page for admin that displays all the different admin control pages.

You can use the truncatechars template filter, e.g.

{{ recent_post.content|truncatechars:300 }} <a href="...">Read more</a>

I’d also recommend to use QuerySet.first() instead of a slice ([:1]) if you want to get the most recent post or None:

recent_post = Posts.objects.filter(status=1).order_by('-created_on').first()
1 Like

thank you very much :smiley: