Question

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))
Was it helpful?

Solution

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 %}

OTHER TIPS

If you absolutely need both views, you could make AJAX calls to update your page content. If you're free to change the code, why not return all needed data from one of both views?

Side note: You should make your code more readable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top