I'm trying to use an HTML template for django mail module. MY current issue is that I'm getting this error:
django.template.exceptions.TemplateDoesNotExist
when I try to render HTML inside my app called users:
html = render_to_string('email/email_confirm.html', context)
Here is my folder layout, my app is called users, my project settings live in /core. My templates are located in BASE_DIR.
Here is my templates code in settings:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
How can I get Django to properly find the templates folder? I have all my apps hooked up and data is working fine. This is strictly a templates path issue.
EDIT: I have kept my APP_DIRS = True and moved the templates/email folder inside the users application folder.
Still django is not finding the template?
Here is the View.py in question:
class CustomUserCreate(APIView):
permission_classes = [AllowAny]
def post(self, request, format='json'):
serializer = CustomUserSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
if user:
# GENERATE EMAIL CONFIRMATION TOKEN
user_data = serializer.data
user = User.objects.get(email=user_data['email'])
token = RefreshToken.for_user(user).access_token
# GENERATE EMAIL CONFIRMATION TEMPLATE
current_site = get_current_site(request).domain
relative_link = reverse('users:email-verify')
# CHANGE TO HTTPS in PRODUCTION
absurl = 'http://'+current_site+relative_link+"?token="+str(token)
email_body = 'Hi '+ user.username+', Please use link below to verify your email \n' + absurl
context = {
'name': user.first_name,
}
html = render_to_string('email/email_confirm.html', context)
text = render_to_string(email_body, context)
data = {'to_email':user.email,
'email_subject': 'Verify your email',
'email_body':email_body,
'message':text,
'html_message':html
}
Util.send_email(data)
return Response(user_data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
EDIT 2: First I tried doing this:
template = get_template('email/email_confirm.html', { 'name': user.first_name})
I got TypeError: unhashable type: 'dict' as an error for the above.
Then I switched it around doing this:
absurl = 'http://'+current_site+relative_link+"?token="+str(token)
email_body = 'Hi '+ user.username+', Please use link below to verify your email \n' + absurl
context = {
'name': user.first_name,
}
template = get_template('email/email_confirm.html')
email = render_to_string(template, context)
data = {'to_email':user.email,
'email_subject': 'Verify your email',
'email_body':email_body,
'html_message':email
}
Util.send_email(data)
Which lead to this error:
raise TypeError(f'{funcname}() argument must be str, bytes, or '
TypeError: join() argument must be str, bytes, or os.PathLike object, not 'Template'
final edit:
data = {'to_email':user.email,
'email_subject': 'Please Verify Your Email',
'email_body':email_body,
'html_message':html
}
from Django can't find my template directory even though it's configured in settings?

No comments:
Post a Comment