Django想要使用另一个视图中的变量
我在名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是添加,因为没有人说过这一点,而且您似乎还不明白这一点:
您的
client_items
视图必须以某种方式访问order_no
变量。如果由于某种原因该值没有通过 URL 传递,则它必须从某个地方获取该值。只有三个真实位置可以获取此值:order_no = Order.objects.filter(cart__user=request.user).order_no
的操作,这将获取与用户当前购物车关联的订单,然后返回order_no
值。order_no
的值,此时您将使用request.session['order_no']=order_no
保存它。稍后,当您想要检索该值时,只需在视图中使用order_no=request.session['order_no']
即可。resp = render_to_response(template_name, locals(), RequestContext(request))
和 then向其中写入一个 cookieresp.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 theorder_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: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 theorder_no
value.order_no
was set, at which point you would save it usingrequest.session['order_no']=order_no
. Later, when you wanted to retrieve the value, you would simply useorder_no=request.session['order_no']
in the view.resp = render_to_response(template_name, locals(), RequestContext(request))
and then write a cookie to itresp.set_cookie("order_no", order_no)
. You retrieve it usingrequest.COOKIES['order_no']
There are other, bizarre, places you could store the value: files, cache, other data storage formats, etc. Not at all recommended.
不。编写一个返回您感兴趣的值的函数,并从两个视图中调用它。
No. Write a function that returns the value you're interested in, and call it from both views.
楼上那位说的很对。您不应该尝试将变量“共享”到不同的视图。
但是,如果您必须或有理由这样做,那么您可以将其存储在会话中,然后您可以在任何有权访问“请求”的视图中访问它。
希望有帮助。
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.