简体   繁体   中英

how to pass a list between views in django

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM