如何在django中的视图之间传递列表 [英] how to pass a list between views in django

查看:99
本文介绍了如何在django中的视图之间传递列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个视图中有一个列表,我想传递给另一个视图被解析。
这是我目前拥有的。
视图:

  def view1(request):
if request.method =='POST' :
list = request.POST.values()
HttpResponseRedirect('/ urls /'+ str(list))

def view2(request,* list):
#do有一个列表

urls:

  urlpatterns = patterns('',
url(r'^ urls / $',views.view1),
url(r'^ urls / (?P< list> [ - / \w] +)$',views.view2),

所以问题是:


  1. 如何形成url regex来识别列表

  2. 如何将列表与HttpResponseRedirect中的其他URL连接起来,以便它将读取

  3. 如何在第二个视图中传递列表(我模糊记住使用*上次我这样做,但我找不到任何有用的参考资料)

编辑:
在更广泛的层次我有一个模板和视图它提供了一个表单中的对象列表。每个对象都被一个复选框选中。我有第二个视图和模板,从第一个视图显示所选对象的数据。我想要选择的对象数量不受限制或限制,但可能不是一个选项。

解决方案

正如布兰登所建议的那样,发布到第二个视图是一个可用的解决方案。
以下行:

  def view2(request):
if request.method == 'POST':
page_list = request.POST.values()
else:
HttpResponseRedirect('/ urls /')

,然后不需要url中的正则表达式


I have a list in one view that I would like to pass to another view to be parsed. This is what I currently have. The views:

def view1(request):
    if request.method=='POST':
        list = request.POST.values()
        HttpResponseRedirect('/urls/'+ str(list)) 

def view2(request, *list):
    #do something with list

the urls:

urlpatterns = patterns('',
    url(r'^urls/$', views.view1),
    url(r'^urls/(?P<list>[-/\w]+)$', views.view2),
)

so the questions are:

  1. how do I form the url regex to recognize the list
  2. how do I concatenate the list with the rest of the url in the HttpResponseRedirect so that it will read
  3. how do i pass the list in the second view (I vaguely remember using * last time I did this but I couldn't find any useful reference material)

EDIT: At the broader level I have a template and view which provide a list of objects in a form. Each object is selected by a checkbox. I have a second view and template that displays data for the selected objects from the first view. I would like the number of objects selected to not be finite or limited but that may not be an option.

解决方案

As Brandon suggested, posting to the second view was a usable solution. Something along the lines of:

def view2(request):
    if request.method == 'POST':
        page_list=request.POST.values()
    else:
        HttpResponseRedirect('/urls/')

and then no need for regex in the urls

这篇关于如何在django中的视图之间传递列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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