views:

62

answers:

2

I have a list of items that looks like this: 'Item 1', 'Item 2', 'Item 3'... with the list being dynamic in length.

My question is how can I pass this variable to my view?

Edit 1 Just thought I'd clarify what I was attempting:

return HttpResponseRedirect(reverse('newFeatures', 
       kwargs={'stock_number': stock_number, 'new_features': new_features}))

With new_features being my dynamic list, and newFeatures being a view that starts like this:

def add_new_feature(request, stock_number, new_features):

Not sure if this is making sense, but I hope it'll help get me out of the dark

A: 

HttpResponseRedirect simply returns an HTTP 302 redirect response, which redirects to another url. You cannot send any kind of POST data with the redirect, so if you want to include any variables, it must be part of the url you redirect to.

If you insist on not processing the list before redirecting, then you best option would probably be to convert the list into a string and use it as a parameter in the url. The newFeatures function could then parse that string back into a list of items.

BernzSed
Actually, if 47 is using a `reverse()` to pass `new_features`, then he/she's using GET, not POST, which would allow parameters to be changed or passed forward.
Mike DeSimone
Yes. Data sent via GET is part of the URL, so you and I are thinking the same thing. Sorry if I was ambiguous.
BernzSed
A: 

How about:

return HttpResponseRedirect(reverse('newFeatures', 
    kwargs={'stock_number': stock_number, 'new_features': ','.join(new_features)}))

and:

def add_new_feature(request, stock_number, new_features_str):
    new_features = new_features_str.split(',')

This assumes the elements in new_features consist only of characters that are safe for URLs, and do not contain commas. If this is not the case, then you'll have to perform escaping of some form.

Bear in mind that it's not recommended for GET-style URLs to change the state of their targets. You should use POST for that, which would prevent you from passing parameters via the URL (i.e. via reverse()). Also, sometimes servers have limits on URL length, which can get in the way of a GET.

Mike DeSimone