Django想要使用另一个视图中的变量

发布于 2024-10-16 22:39:39 字数 731 浏览 1 评论 0原文

我在名为 client_items 的视图中有一个项目列表。我希望能够使用变量 items_list`,这是 client_items 中另一个名为 edit_order 的视图。那么有没有办法从不同的视图调用变量呢? (从另一个视图导入变量并能够在另一个视图中使用该变量)我不能只将其写入 client_items 视图中,因为它需要 order_no 增强。

编辑:这是我的最新观点。我尝试创建另一个名为 items_in_edit_order 的视图。此时我得到`order_no not Defined。

    def items_in_edit_order(order_no):
        order = models.Order.objects.get(pk = order_no)
        return order

def client_items(request, client_id = 0):
    client = models.Client.objects.get(pk = client_id)
    items = client.storageitem_set.all()
    order = items_in_edit_order(order_no)
    return render_to_response('items.html', {'items':items, 'client':client, 'order':order}, context_instance = RequestContext(request))

I have a list of items in a view called client_items. I want to be able to use the variable items_list`which is another view called edit_order in client_items. So is there a way to call the variable from a different view? (Import a variable from another view and be able to use this variable in the other) I cannot just write it in client_items view because it needs an order_no augment.

Edit: here is my latest views. I have tried creating another views called items_in_edit_order. At this point I get `order_no not defined.

    def items_in_edit_order(order_no):
        order = models.Order.objects.get(pk = order_no)
        return order

def client_items(request, client_id = 0):
    client = models.Client.objects.get(pk = client_id)
    items = client.storageitem_set.all()
    order = items_in_edit_order(order_no)
    return render_to_response('items.html', {'items':items, 'client':client, 'order':order}, context_instance = RequestContext(request))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒七 2024-10-23 22:39:39

只是添加,因为没有人说过这一点,而且您似乎还不明白这一点:

您的 client_items 视图必须以某种方式访问​​ order_no 变量。如果由于某种原因该值没有通过 URL 传递,则它必须从某个地方获取该值。只有三个真实位置可以获取此值:

  1. 数据库:如果您要存储诸如直接链接到用户的购物车之类的东西,这将起作用。例如,您可能可以执行类似 order_no = Order.objects.filter(cart__user=request.user).order_no 的操作,这将获取与用户当前购物车关联的订单,然后返回order_no 值。
  2. 会话:您将 order_no 存储在会话中。这假设您有一个较早的视图,其中设置了 order_no 的值,此时您将使用 request.session['order_no']=order_no 保存它。稍后,当您想要检索该值时,只需在视图中使用 order_no=request.session['order_no'] 即可。
  3. Cookie:并不真正推荐,但仍然是一个选择。这很复杂,因为在第一个视图中,您必须创建响应对象(如 resp = render_to_response(template_name, locals(), RequestContext(request))then向其中写入一个 cookie resp.set_cookie("order_no", order_no)。您可以使用 request.COOKIES['order_no'] 检索它

。可以存储值:文件、缓存、其他数据存储格式等。完全不推荐。

Just adding, since no one has said this and it seems like you don't understand this yet:

Your client_items view must, somehow, have access to the order_no variable. If for some reason the value is not being passed along via the URL, it must get the value from somehwere. There are only three real locations where it could get this value:

  1. Database: this will work if you are, for example, storing something like a cart which is directly linked to a user. So for example, you might be able to do something like order_no = Order.objects.filter(cart__user=request.user).order_no which would get the order associated with the user's current cart, then return the order_no value.
  2. Session: you store the order_no in the session. This would assume you had an earlier view where the value for order_no was set, at which point you would save it using request.session['order_no']=order_no. Later, when you wanted to retrieve the value, you would simply use order_no=request.session['order_no'] in the view.
  3. Cookie: not really recommended, but an option nonetheless. It's complicated because in the first view you'd have to create the response object (as in resp = render_to_response(template_name, locals(), RequestContext(request)) and then write a cookie to it resp.set_cookie("order_no", order_no). You retrieve it using request.COOKIES['order_no']

There are other, bizarre, places you could store the value: files, cache, other data storage formats, etc. Not at all recommended.

不甘平庸 2024-10-23 22:39:39

不。编写一个返回您感兴趣的值的函数,并从两个视图中调用它。

No. Write a function that returns the value you're interested in, and call it from both views.

蹲墙角沉默 2024-10-23 22:39:39

楼上那位说的很对。您不应该尝试将变量“共享”到不同的视图。

但是,如果您必须或有理由这样做,那么您可以将其存储在会话中,然后您可以在任何有权访问“请求”的视图中访问它。

希望有帮助。

What the guy said above is correct. You shouldn't attempt to "share" variables to different views.

However in the event you must or have a reason to then you could just store it in the session and then you have access to it in any view that has access to the "request".

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文