想从另一个视图django打印出一个项目列表 [英] Want to print out a list of items from another view django

查看:239
本文介绍了想从另一个视图django打印出一个项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示列表项的视图。

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))

我以为也许我可以粘贴项目的定义,只是调用该item_list但是不起作用。任何想法

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

items.html

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


推荐答案

从您的评论中:


我得到一个白色屏幕,屏幕上打印了URL。 / tiptop / client在这种情况下。

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

因为这是你要求的:

except:
    return HttpResponse(reverse(return_clients))

意味着如果上面有任何错误或问题,您的视图将只输出一个包含该URL的响应。也许你打算使用 HttpResponseRedirect ,所以浏览器实际上重定向到URL - 但是你仍然不应该使用一个空白的,除了

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.

若要回答主要问题,请考虑您的 edit_order 视图返回:它给你一个完整的HTML响应与渲染的模板。如何在另一个视图中将其用作查询中的元素?您需要在逻辑上考虑这一点。

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?

这篇关于想从另一个视图django打印出一个项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆