how to combine image and videoembed in listview django

I have two class model image and videoembed. the question is how to put two class in single listview… when there’s no image, video embed show up it’s my code.

in views.py

def news_list(request):
"""news list category"""
category = Category.objects.get(id=1)
a_list = Article.objects.filter(category=1)
g_list = Gallery.objects.filter(category=1)
v_list = Videoembed.objects.filter(category=1)
object_list = sorted(
    chain(a_list, g_list, v_list), key=attrgetter("publish"), reverse=True
)
paginator = Paginator(object_list, 4)  # 3 posts in each page
page = request.GET.get("page")
try:
    posts = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer deliver the first page
    posts = paginator.page(1)
except EmptyPage:
    # If page is out of range deliver last page of results
    posts = paginator.page(paginator.num_pages)

return render(
    request,
    "blog/post/news.html",
    {"category": category, "page": page, "posts": posts},
)

news.html

<div class="row">
   {% for post in posts %}
    <div class="col-lg-4 col-md-6 col-sm-12 pb-4">
       {% if forloop.counter0|divisibleby:3 and not forloop.first %}<div class="w-100"></div>{% endif %}
      {% video post.video  %}
       <img src="{{ post.images.url }}" width="300px">
      <P>
         <h2><a href="{{ post.get_absolute_url }}">
           {{ post.title }}
         </a> </h2>  </p>
         <p class="date">
           Published {{ post.publish }} by {{ post.author }}
           </p>
           {{ post.body|safe|truncatewords:30|linebreaks }}

       </div>
   {% endfor %}


thanks for any help

I don’t understand what your question is here. It looks like you’ve got some code that might do what you want.
If it’s not, it’ll be helpful if you let us know what’s not working with it. Are you getting some type of error message? Are you not seeing something you’re expecting to see? (Or seeing something you shouldn’t be seeing?)

Do all those models have the same attributes, such that the references to them in the template are going to work?

there are two class model, article class with image and videoembed class model. joining two class in one page listview you can look in in views.py with news.html template for output.

the problem is how to hide image in video embed post. its what i got

It looks like you’re explicitly showing the video in your template. If you don’t want the video to be rendered, remove it from the template.

yess i tried to configure with if and for but unsuccess … :face_with_thermometer:

if videoembed show hide image

Ok, If I can try to rephrase your question to see if I understand the issue, you saying that if the model type is Videoembed, then you do not want to display the image as identified here:
<img src="{{ post.images.url }}" width="300px">

Is that correct?

yess thats correct sirr

Ok, so assuming that Videoembed is the only type with the video attribute, you could try something like this:

{% if post.video %}
{% video post.video  %}
{% else %}
<img src="{{ post.images.url }}" width="300px">
{% endif %}

Thank you for the solution, it works for me. I have did something like that before, maybe I missed some phrase. Thank you so much for your help