I am working on a group project for college using django and python, however I am running into an issue.
I am following a newsletters tutorial from a youtube playlist and while he has no errors this is the error I am receiving.
Here is the code.
def newsletter_signup(request): form = newsletterUserSignUpForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
if newsletterUser.objects.filter(email = instance.email).exists():
messages.warning(request, 'Your email already exists in our database', 'alert alert-warning alert-dismissible')
else:
instance.save()
messages.success(request, 'Your email has been signed up to our Newsletter!', 'alert alert-success alert-dismissible')
subject = "Thank you for joining our Newsletter"
from_email = settings.EMAIL_HOST_USER
to_email = [instance.email]
with open(settings.BASE_DIR "/templates/newsletters/sign_up_email.txt") as f:
sign_up_message = f.read()
message = EmailMultiAlternatives(subject=subject, body=signup_message, from_email = from_email, to_email = to_email)
html_template = get_template("/templates/newsletters/sign_up_email.html").render()
message.attach_alternative(html_templae, "text/html")
message.send()
context = {
'form': form,
}
template = "newsletters\sign_up.html"
return render(request, template, context)
I realise the video may be outdated a tad as its from 2017 so any help would be appreicated.
CodePudding user response:
Around django-3.0 the BASE_DIR
setting started to use a Path
[Python-doc] instead of a simple string, and you can not append a Path
with a string. You can however use /
operator [Python-doc] to join the path with a directory or filename, so you can use:
with open(settings.BASE_DIR / 'templates/newsletters/sign_up_email.txt') as f:
# …
pass
CodePudding user response:
You might want to try using os.path.join. This functions builds paths that match your operating system.
import os
path = os.path.join(settings.BASE_DIR,"/templates/newsletters/sign_up_email.txt")
with open(path) as f:
# DO STUFF