python - Issue encoding an email for my app -


i configuring app operates subscriptions emails, runs except part of form should encode email received.

this error obtained when sign email:

**unicode-objects must encoded before hashing**  traceback: file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response   132. response = wrapped_callback(request, *callback_args, **callback_kwargs)  file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/views/generic/base.py" in view   71. return self.dispatch(request, *args, **kwargs)  file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch   89. return handler(request, *args, **kwargs)  file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/views/generic/edit.py" in post   214. if form.is_valid():  file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/forms/forms.py" in is_valid   184. return self.is_bound , not self.errors  file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/forms/forms.py" in errors   176. self.full_clean()  file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/forms/forms.py" in full_clean   393. self._clean_form()  file "/home/draicore/sunflower/ambiente1/lib/python3.4/site-packages/django/forms/forms.py" in _clean_form   417. cleaned_data = self.clean()  file "/home/draicore/sunflower/girasol/apps/newsletter/forms.py" in clean   47. self.cleaned_data['activation_key'] = generate_activation_key(data['email'])  file "/home/draicore/sunflower/girasol/apps/newsletter/forms.py" in generate_activation_key   16. salt = hashlib.md5(str(random.random())).hexdigest()[:10]  exception type: typeerror @ /request/  exception value: unicode-objects must encoded before hashing 

i have code responsible of encoding emails on forms.py:

def generate_activation_key(email):     if isinstance(email, str):         email = email.encode('utf-8')     salt = hashlib.md5(str(random.random()).encode('utf-8')).hexdigest()[:10]     return hashlib.md5(salt+email).hexdigest()   class emailsubscriberform(forms.modelform):     email = forms.emailfield(max_length=256, label=_('email'), required=true)     captcha = captchafield(label=_('security code'))     activation_key = forms.charfield(widget=forms.hiddeninput(), required=false)     activation_request_sent_at = forms.datefield(widget=forms.hiddeninput(), required=false)      class meta:         model = emailsubscriber         fields = (             'email',             'activation_key',             'activation_request_sent_at',         )      def clean_email(self):         email = self.cleaned_data['email'].strip()         try:             self.instance = emailsubscriber.objects.get(email__iexact=email)             if self.instance not none:                 return email.lower()         except emailsubscriber.doesnotexist:             return email.lower()         raise forms.validationerror(_('this email subscribed.'))      def clean(self):         data = self.cleaned_data         if 'email' in data:             self.cleaned_data['activation_key'] = generate_activation_key(data['email'])             self.cleaned_data['activation_request_sent_at'] = timezone.now()         return self.cleaned_data 

i believe issue caused incompatibility of version of python (i have python 3.4.1) , trying looking solutions here in other questions , found answers new format set .encode('utf-8'). , dont know should change.

apologizeme in advance if overlook something. contribution wellcome, evaluate!

have great day!!

you should encode random salt string:

str(random.random()).encode('utf-8') 

however, you'll receive new error...because email bytes , salt string. i'll leave determine how want normalize these can added together.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -