-
-
Save ijharulislam/28e9743e68a165dff0e4d1669644c4ba to your computer and use it in GitHub Desktop.
Django html input array and dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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': ...} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great solution !
I've updated your code to get multi-dimentionnal arrays :