Skip to content

Instantly share code, notes, and snippets.

@MasterAlish
Last active June 29, 2020 17:58
Show Gist options
  • Save MasterAlish/dac9158b4dbdfa950840 to your computer and use it in GitHub Desktop.
Save MasterAlish/dac9158b4dbdfa950840 to your computer and use it in GitHub Desktop.
Django html input array and dict
def get_html_input_dict(self, query_dict, param):
dictionary = {}
regex = re.compile('%s\[([\w\d_]+)\]' % param)
for key, value in query_dict.items():
match = regex.match(key)
if match:
inner_key = match.group(1)
dictionary[inner_key] = value
return dictionary
# query_dict - request.POST or request.GET
# param - html parameter name
#
# Use example:
# <form method="post">
# <input type='text' name='student[1]'>
# <input type='text' name='student[2]'>
# <input type='text' name='student[3]'>
# ...
# </form>
#
# In django views do next:
# students = get_html_input_dict(request.POST, 'student')
#
# It returns:
# students => {'1': ... , '2': ..., '3': ...}
@yooslim
Copy link

yooslim commented Jun 29, 2020

You would probably escape "param" to allow something like:

class_1[student][1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment