0

I have added my view and template codes below. MY question, how can i get bird( model 1) and respective bird's details(model2) --------view------

all_birds = Bird.objects.all()
detail = Detail.objects.all()
template = loader.get_template('bird/bird.html')
contex = {
    'all_birds': all_birds,
    'details': detail,
}
return HttpResponse(template.render(contex, request))

--------template----------

{% for birds in all_birds %}
        <div class="container">
            <div class="row">
                  <div id="board" data-columns>
                    <div class="item">
                        <div class="ani-box">
                            <a href="{% static 'bird/images/img_10.jpg'%}" class="image-popup fh5co-board-img">
                                 <img src="{% static 'bird/images/img_10.jpg'%}" alt="No Image available"></a>
                        </div>
                    </div>
                  </div>
             </div>
        </div>
    {% endfor %}
Insp
  • 11
  • 1

1 Answers1

0

You will need to get each bird's respective details, assuming they're related by foreign key.

If you have models for 'Widget' and 'Specs' for example like so:

class Widget(models.Model):
    name = models.CharField(max_length=255)
    specs = models.ForeignKey('Specs')

class Specs(models.Model):
    foo = models.IntegerField()
    bar = models.UrlField()

If have a context of all widgets, i.e. {'widgets': Widget.objects.all()}, as you iterate over them you have access to their Specs object.

{% for widget in widget %}
   <ul>
   <li>{{ widget.name }}</li>
   <li>{{ widget.specs.foo }}</li>
   <li>{{ widget.specs.bar }}<li>
   </ul>
{% endfor %}
Benjamin Hicks
  • 756
  • 3
  • 7