Last active
June 29, 2020 17:58
-
-
Save MasterAlish/dac9158b4dbdfa950840 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
You would probably escape "param" to allow something like:
class_1[student][1]