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?
#models.py
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
#views.py
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)
#admin_edits.html
<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 %}
</div>
<a href="{% url 'editors:blog_editor' %}">Go to full blog editor</a>
</section>
<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 %}
</div>
<a href="{% url 'editors:calendar_events' %}">Go to full calendar</a>
</section>
<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 %}
</div>
<a href="{% url 'editors:gallery_editor' %}">
</section>
<section id="users">
<div id="new_users">
{% if new_users %}
{% for user in new_users %}
<ul>
<li><strong>Username:</strong> {{ user.username }}</li>
<li><strong>Name:</strong> {{ user.first_name }} {{ user.last_name }}
</ul>
<br />
{% endfor %}
{% else %}
<p>There are no users on this system.</p>
{% endif %}
</div>
<a href="{% url 'editors:user_control' %}">
</section>
for context, this is the home page for admin that displays all the different admin control pages.