开发者

how to pass a list between views in django

开发者 https://www.devze.com 2023-03-14 01:02 出处:网络
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.

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(lis开发者_运维知识库t)) 

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


I would like the number of objects selected to not be finite or limited but that may not be an option.

It definitely is an option. Capture everything post the certain word as a single reg-ex and parse it to different "tags" within your view.

0

精彩评论

暂无评论...
验证码 换一张
取 消