Using Django 3.2
Have defined few global variables like
app/context_processors.py
from app.settings import constants
def global_settings(request):
return {
'APP_NAME': constants.APP_NAME,
'APP_VERSION': constants.APP_VERSION,
'STATIC_URL_HOST': constants.STATIC_URL_HOST
}
and in settings.py
file
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'app.context_processors.global_settings',
],
},
},
]
Have used APP_NAME
in the email template footer like
account/emails.py
def welcome_email(user):
subject_file = 'account/email/welcome_subject.txt'
body_text_file = 'account/email/welcome_message.txt'
body_html_file = 'account/email/welcome_message.html'
subject_text = get_template(subject_file)
body_text = get_template(body_text_file)
body_html = get_template(body_html_file)
context = {
'username': user.username,
}
subject_content = subject_text.render(context)
body_text_content = body_text.render(context)
body_html_content = body_html.render(context)
to = [user.email]
msg = EmailMultiAlternatives(
subject=subject_content,
body=body_text_content,
from_email='{} <{}>'.format('Admin', 'admin@example.com'),
to=to,
)
msg.attach_alternative(body_html_content, 'text/html')
msg.send()
templates/account/welcome_message.html
Hi {{ username }},
Welcome to the application.
{{ APP_NAME }}
When the email is sent from the web portal, the APP_NAME
is rendered fine, but when the email send is initiated from the Django shell
python manage.py shell
> from account.emails import welcome_email
> welcome_email(user)
Then the APP_NAME
is not rendered in the email.
How the context processor can be rendered from the shell as well?