Django AttributeError: 'tuple' object has no attribute 'splitlines'
I'm trying to create an user registration with email confirmation and came up with this code in the models.py
class UserRegister(SuccessMessageMixin, FormView):
template_name = 'login/form_register.html'
form_class = UserRegisterForm
redirect_authenticated_user = True
success_url = reverse_lazy('tasks')
success_message = "User has been created, please login"
def form_valid(self, form):
user = form.save(commit=False)
user.is_active = False # Deactivate account till it is confirmed
user.save()
current_site = get_current_site(self.request)
subject = 'Activate Your Account'
message = render_to_string('login/account_activation_email.html'), {
'user':user,
'domain':current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
}
user.email_user(subject, message)
messages.add_message(
self.request,
messages.SUCCESS,
'Check Your Email For Account Activation Link'
)
if user is not None:
login(self.request, user)
return super(UserRegister, self).form_valid(form)
def get(self, *args, **kwargs):
if self.request.user.is_authenticated:
return redirect('tasks')
return super(UserRegister, self).get(*args, **kwargs)
But I keep getting this error AttributeError: 'tuple' object has no attribute 'splitlines'
This is the traceback
Internal Server Error: /register/
Traceback (most recent call last):
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/views/generic/base.py", line 142, in dispatch
return handler(request, *args, **kwargs)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/views/generic/edit.py", line 153, in post
return self.form_valid(form)
File "/home/ihzacordova/projects/todo-list/login/models.py", line 52, in form_valid
user.email_user(subject, message)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/contrib/auth/models.py", line 402, in email_user
send_mail(subject, message, from_email, [self.email], **kwargs)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/mail/__init__.py", line 87, in send_mail
return mail.send()
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/mail/message.py", line 298, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 131, in send_messages
sent = self._send(message)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 147, in _send
message = email_message.message()
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/mail/message.py", line 260, in message
msg = SafeMIMEText(self.body, self.content_subtype, encoding)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/mail/message.py", line 160, in __init__
MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset)
File "/usr/lib/python3.8/email/mime/text.py", line 42, in __init__
self.set_payload(_text, _charset)
File "/home/ihzacordova/.local/share/virtualenvs/todo-list-KiFNCv1Z/lib/python3.8/site-packages/django/core/mail/message.py", line 170, in set_payload
for line in payload.splitlines()
AttributeError: 'tuple' object has no attribute 'splitlines'
Change
message = render_to_string('login/account_activation_email.html'), {
'user':user,
'domain':current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
}
To
message = render_to_string('login/account_activation_email.html', {
'user':user,
'domain':current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
})