如何在django中的视图之间传递列表 [英] how to pass a list between views in 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),
)
所以问题是:
- 如何形成url regex来识别列表
- 如何将列表与HttpResponseRedirect中的其他URL连接起来,以便它将读取
- 如何在第二个视图中传递列表(我模糊记住使用*上次我这样做,但我找不到任何有用的参考资料)
编辑:
在更广泛的层次我有一个模板和视图它提供了一个表单中的对象列表。每个对象都被一个复选框选中。我有第二个视图和模板,从第一个视图显示所选对象的数据。我想要选择的对象数量不受限制或限制,但可能不是一个选项。
正如布兰登所建议的那样,发布到第二个视图是一个可用的解决方案。
以下行:
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:
- how do I form the url regex to recognize the list
- how do I concatenate the list with the rest of the url in the HttpResponseRedirect so that it will read
- 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屋!