I am sending an email from Django using the celery. the email was going out and all of a sudden I started getting below mention error, I am unable to figure out what is cause it. please help out. Thanks in advance.
email.py
from django.conf import settings
from django.core.mail import EmailMessage
from django.template import Context
from django.template.loader import render_to_string
def send_pam_request_email(email, message):
c = Context({'email': email, 'message': message})
email_subject = render_to_string(
'pam/email/pam_email_subject.txt', c).replace('\n', '')
email_body = render_to_string('pam/email/pam_email_body.txt', c)
email = EmailMessage(
email_subject, email_body, [settings.DEFAULT_FROM_EMAIL], [email]
)
return email.send(fail_silently=False)
view.py
email = get_approvers_email()
messeage_new = """
New Request from {} that needs your attention.
url/{}/
""".format(requester,slug)
send_new_request_email_task.delay(email, messeage_new)
error
[2020-06-10 08:34:22,773: INFO/MainProcess] Received task: send_new_request_email_task[ef9dbf20-6074-43e2-94dd-837f096bfeac]
[2020-06-10 08:34:22,785: INFO/ForkPoolWorker-7] send_new_request_email_task[ef9dbf20-6074-43e2-94dd-837f096bfeac]: Sent New PAM request email to the approvers
[2020-06-10 08:34:22,810: ERROR/ForkPoolWorker-7] Task send_new_request_email_task[ef9dbf20-6074-43e2-94dd-837f096bfeac] raised unexpected: TypeError('context must be a dict rather than Context.',)Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/celery/app/trace.py", line 412, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/celery/app/trace.py", line 704, in __protected_call__
return self.run(*args, **kwargs)
File "/code/web/pam/tasks.py", line 13, in send_new_request_email_task
return send_pam_request_email(email, message)
File "/code/web/pam/emails.py", line 11, in send_pam_request_email
'pam/email/pam_email_subject.txt', c).replace('\n', '')
File "/usr/local/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/usr/local/lib/python3.6/site-packages/django/template/backends/django.py", line 59, in render
context = make_context(context, request, autoescape=self.backend.engine.autoescape)
File "/usr/local/lib/python3.6/site-packages/django/template/context.py", line 270, in make_context
raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than Context.
just use dict for render_to_string django docs
c = {'email': email, 'message': message}