Django UKPhoneNumberField problem!
I'm currently using UKPhoneNumberField in one of my forms. I recently found that if I submit a London number 02081112222
(or as 020 8111 2222
) it saves the number as 020 81112222 8111
in the DB. I'm not very good at debu开发者_运维技巧gging python so I was wondering if someone could help me track down the problem. Cheers
EDIT:
I sure it is something to do with the format_number
function since I commented it out my problems go away (yes, I know it's not a solution, just a step closer to one):
def format_number(self, value, number_spec):
# if number_spec[1] is None:
# components = (value,)
# else:
# components = []
# position = 0
# last_index = len(number_spec) - 1
# for index, chunk in enumerate(number_spec[1]):
# if index == last_index:
# components.append(value[position:])
# else:
# components.append(value[position:position+chunk])
# position += chunk
# return ' '.join(components)
return value
Debugging it is going to be hard because I have know idea what it does. (Trust me I'm really trying!! I'm just new to this whole python stuff)
The problem looks likes its caused by the following in format_number
last_index = len(number_spec) - 1
As the code loops the second (or last -1) iteration will match, this results in the code appending 81112222 (position:) to the string rather then 8111, the 3rd iteration then adds 8111. Removing - 1 form the code solves this however I'm guessing this will affect the field in others ways.
You may not be good as debugging python, but this is your chance to learn. Try stepping through it with pdb (it's fairly well documented). If you can give a code-snippet including your model definition and the code adding that number to your db, I'll see if I can give you a hand.
Manual debugging is a skill you're going to need to develop if you want to get very far with these sorts of problems.
I tried:
def format_number(value, number_spec):
components = []
position = 0
last_index = len(number_spec) - 1
for index, chunk in enumerate(number_spec):
if index == last_index:
components.append(value[position:])
else:
components.append(value[position:position+chunk])
position += chunk
print components
print format_number('02081112222', (3, 4, 4))
obviously I've tweaked it to not need the rest of the code, but this seems to be working fine...
精彩评论