UserCreationForm decode str object has not decode attribute

226 views
Skip to first unread message

Ahmet İnal

unread,
Jul 18, 2019, 9:28:31 AM7/18/19
to Django users


What's wrong taht code if i try register i seeing.


AttributeError at /register/

'str' object has no attribute 'decode'
'uid':urlsafe_base64_encode(force_bytes(user.pk)).decode('utf-8'), whats problem on this line.

class MyUserCreationForm(UserCreationForm):
class Meta:
# Yeni Model
model = User
# Yeni alanlar
fields = { 'username', 'password1', 'password2', 'email',}
exclude = ['Avatar','gender','birth_date',]





def clean_username(self):
username = self.cleaned_data['username'].lower()
if username in blacklist:
raise ValidationError("Lütfen başka bir kullanıcı adı seçin.")
return username

def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=True)
current_site = Site.objects.get_current()
mail_subject = 'Activate your blog account.'
message = render_to_string('includes/emails/activation-mail.html', {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)).decode('utf-8'),
'token':account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()

Aldian Fazrihady

unread,
Jul 18, 2019, 8:06:36 PM7/18/19
to django...@googlegroups.com
url_safe_base64_encode returns string, and strings don't have decode method.
However, bytes objects have decode method.

string.encode will change string to bytes.
bytes.decode will change bytes to string.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0b4256b7-080a-4f85-b4e3-e741ec8447ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Regards,

Reply all
Reply to author
Forward
0 new messages