I have two models
class Post(models.Model):
title = models.CharField(max_length=100)
body = RichTextField(max_length=1000000)
created_at = models.DateTimeField(default=datetime.now, blank = True)
image = ResizedImageField(size=[250, 200], upload_to='img')
and
class Politics(models.Model):
title = models.CharField(max_length=100)
body = RichTextField(max_length=1000000)
created_at = models.DateTimeField(default=datetime.now, blank = True)
image = ResizedImageField(size=[250, 200], upload_to='img',blank = True)
I want to combine them both into one template view and render them on the index.html Here is my view function
def index(request):
politics = Politics.objects.all()
return render(request,
'index.html',
{'politics':politics, 'posts': Post.objects.all()})
index.html (posts part)
<section id="posts" >
<div data-aos="fade-up">
<div >
{% for post in posts reversed %}
{% if forloop.counter < 5 %}
<div >
<a href="/post/{{post.id}}"><img src="{{post.image.url}}" ></a>
<div>
<div ><span >{{post.category}}</span> <span >•</span> <span>{{post.created_at}}</span></div>
<h2><a href="/post/{{post.id}}">{{post.title}}</a></h2>
</div>
<p >{{post.body|truncatewords:75}}</p>
<div >
<div ><img src="{% static 'assets/img/person-1.jpg' %}" alt="" ></div>
<div >
<h3 >OlePundit</h3>
</div>
</div>
</div>
(politics part)
<div >
{% for politic in politics%}
{% if forloop.counter < 11 %}
<div >
<a href="/politicalpost/{{politic.id}}"><img src="{{politic.image.url}}" alt="" ></a>
<div >
<span >{{politic.category}}</span>
<span >•</span>
<span>{{politic.created_at}}</span>
</div>
<h2 ><a href="/politicalpost/{{politic.id}}">{{politic.title}}</a></h2>
<span >Ole Pundit</span>
<p >{{politic.body| safe | truncatewords:20}}</p>
</div>
{% endif %}
{% endfor %}
</div>
However, only the 'politics' object is being rendered. What could be wrong?
CodePudding user response:
try this or the problem will be in template file
def index(request):
politics = Politics.objects.all()
posts = Post.objects.all()
return render(request,
'index.html',
{'politics':politics, 'posts': posts})