How do I load multiple models items into a single view

I’m using a model to load items from a model to the sidebar and I want to use another model to load an item into a table in thesame view (page). Please I need urgent attention.
Thanks

you can import the models in the views.py and also query all your model you need in same view.

Example:

from mobiles.models import Mobile
from laptops.models import Laptop

def view_items(request):
       mobiles = Mobile.objects.all()
       laptops = Laptop.objects.all()
       context = {
              'mobiles': mobiles,
              'laptops': laptops
       }
       return render(request, 'view_item.html', context)

You can access each object in the same view by this way:

For Mobiles:

{% for mobile in mobiles %}
         <p>{{ mobile }}</p>
{% endfor %}

For Laptops:

{% for laptop in laptops %}
         <p>{{ laptop }}</p>
{% endfor %}
1 Like