You've got a json
structured array, why don't you want to send it to your view as a file? You can submit a file through your form, and get it with request.FILES
.
Well, to answer your question which is more a python
than a django
question actually:
def transform_for_url(array_to_process):
array_copy = str(array_to_process)
to_replace = {
"'": "",
'[': '',
']': '',
'{': '',
'},': ';',
'}': '',
' ': ''
}
for key, val in to_replace.items():
array_copy = array_copy.replace(key, val)
return array_copy
def get_back_from_url(url_to_process):
new_list = []
dicts = url_to_process.split(';')
for single_dict in dicts:
new_dict = {}
for elt in single_dict.split(','):
elt = elt.split(':')
new_dict.update({elt[0]: elt[1]})
new_list.append(new_dict)
return new_list
x = [
{'aa': '123', 'bb': '456','cc': '798'},
{'aa': '111', 'bb': '222','cc': '333'},
{'aa': 'a1', 'bb': 'b2','cc': 'c3'}
]
y = transform_for_url(x)
print(y)
>>> aa:123,bb:456,cc:798;aa:111,bb:222,cc:333;aa:a1,bb:b2,cc:c3
print(get_back_from_url(y))
>>> [{'aa': '123', 'bb': '456', 'cc': '798'}, {'aa': '111', 'bb': '222', 'cc': '333'}, {'aa': 'a1', 'bb': 'b2', 'cc': 'c3'}]
Not the most efficient way to do it, but still does the job!