Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Difference between cleaned_data and cleaned_data.get in Django
- def clean_message(self):
- message = self.cleaned_data['message']
- num_words = len(message.split())
- if num_words < 4:
- raise forms.ValidationError("Not enough words!")
- return message
- def clean(self):
- username = self.cleaned_data.get('username')
- password = self.cleaned_data.get('password')
- ...
- self.check_for_test_cookie()
- return self.cleaned_data
- >>> cleaned_data = {'username': "bob", 'password': "secret"}
- >>> cleaned_data['username']
- 'bob'
- >>> cleaned_data.get('username')
- 'bob'
- >>> cleaned_data['foo']
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- KeyError: 'foo'
- >>> cleaned_data.get('foo') # No exception, just get nothing back.
- >>> cleaned_data.get('foo', "Sane Default")
- 'Sane Default'
Add Comment
Please, Sign In to add comment