Answer the question
In order to leave comments, you need to log in
How to implement post parameter handling if you don't know its name?
I need to write a handler that accepts different post parameters that have different variable names all the time.
that is, the handler must add a phone number to the database, but it can come with both POST['phone'] and POST['telephone'], etc.
From my thoughts there are two ways. This is to store, for each field you expect, possible variable names. That is, I expect phone_number = ['telephone','phone'] for the example above. And see if there is such a variable in the data sent by POST, if not, then the second method is turned on, this is a search by pattern (regex). We search by the values of the data sent by POST and thus calculate the variable name.
Are there more delicate, slender ways of implementation?
Answer the question
In order to leave comments, you need to log in
Well, there must be some common feature. If this is the substring "phone" or there - "phone" - then you can do something like this:
data1 = {'key': '1', 'phone': '2'}
data2 = {'key': '3', 'telephone': '4'}
data3 = {'key': '5', 'telePhone': '6'}
data4 = {'key': '7', 'PHONE': '8'}
data5 = {'key': '9', 'Телефон': '10'}
PATTERNS = ['phone', 'телефон'] # тут перечислить подстроки
getphone = lambda data:next(filter(lambda x: any([y in x.lower() for y in PATTERNS]), data.keys()))
print(data1[getphone(data1)])
print(data2[getphone(data2)])
print(data3[getphone(data3)])
print(data4[getphone(data4)])
print(data5[getphone(data5)])
# 2
# 4
# 6
# 8
# 10
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question