Django 1.6 Obtain data from two models in one view

advertisements

Have two models and need to get them both out on one page at the same time. I know I need one view with the two models under it.

lol

heres what I got:

def inventory(request):
    products = Product3.objects.all()

    productinfo = {
    "product_detail": products
    }
    return render_to_response('inventory.html', productinfo, context_instance=RequestContext(request))

def itemdetailpage(request, id):
    property = Product3.objects.get(pk=id)
    property1 = property.images.all()
    itemin = {"itemdetail": property1 }
    return render_to_response('details.html', itemin, context_instance=RequestContext(request))

How do I get these data into one view? so I can display both of their contents in one template, with the arguments part of itemdetail page code.?

Solution in the view has been as follows: I figured this out about an hour after I posted this and thank you Dan for the responding and are absolutely right everything was there:

def itemdetailpage(request, id):
    property = Product3.objects.get(pk=id)
    property1 = property.images.all()
    itemin = {'property': property, 'imagedetail': property1}
    return render_to_response('details.html', itemin, context_instance=RequestContext(request))


You have all the information you need already. You know how to get the images related to an item, with product.images.all(). You can simply do that in the template, as you iterate through the products; you don't need the second view at all.

{% for product in products %}
    {{ product.name }}
    {% for image in product.images.all %}
        {{ image.image }}
    {% endfor %}
{% end for %}