Want to print out a list of items from another view django

一个人想着一个人 提交于 2019-12-25 04:32:05

问题


I have a view which displays a list items.

def edit_order(request, order_no):

try:
    status_list = models.Status.objects.all()
    order = models.Order.objects.get(pk = order_no)
    if order.is_storage:
        items = models.StorageItem.objects.filter(orderstoragelist__order__pk = order.pk)
    else:
        items = models.StorageItem.objects.filter(orderservicelist__order__pk = order.pk)
except:
    return HttpResponseNotFound()

I want to put these list of item in another view. Unfortunately this is proving to be trickier then I thought.

@login_required
def client_items(request, client_id = 0):
    client = None
    items = None
    try:
        client = models.Client.objects.get(pk = client_id)
        items = client.storageitem_set.all()
        item_list = models.StorageItem.objects.filter(orderstoragelist__order__pk = order.pk)
    except:
        return HttpResponse(reverse(return_clients))
    return render_to_response('items.html', {'items':items, 'client':client, 'item_list':item_list}, context_instance = RequestContext(request))

I thought maybe I can just paste the definition of items and just call that item_list but that does not work. Any ideas

items.html

{% for item in item_list %}
    {{item.tiptop_id}
{% endfor %}

回答1:


From your comment:

I get a white screen with the url printed on the screen. /tiptop/client in this case.

Because that's what you've asked for:

except:
    return HttpResponse(reverse(return_clients))

This means that if there are any bugs or problems in the above, your view will simply output a response containing just that URL. Maybe you meant to use HttpResponseRedirect, so the browser actually redirects to the URL - but still you should not use a blank except, as it prevents you from seeing what is actually going wrong.

To answer the main question, think about what your edit_order view returns: it gives you a complete HTML response with a rendered template. How could you use that as an element in a query in another view? You need to think logically about this.

One possible solution would be to define a separate function which just returns the data you want - as a plain queryset - and both views can call it. Does that do what you want?



来源:https://stackoverflow.com/questions/4935019/want-to-print-out-a-list-of-items-from-another-view-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!