I'm attempting to add a success or error message to a form. As far as I can
see, I've added everything correctly. But, when I try to submit the form
with success or with error I get a 'NameError at /contact/' -> 'name
message not defined'. Note that it says name message (not plural) not
defined. Even though I only call messages. Here is my code
# views.py
from django.shortcuts import render, redirect
from django.contrib import messages
from .models import Employee
from .forms import EmailForm
from django.core.mail import send_mail
from django.template import Context
from django.template.loader import get_template
# Create your views here.
def home(request):
return render(request, 'index.html')
def team(request):
members = Employee.objects.all()
return render(request, 'team.html', {'members':members})
def contact(request):
form_class = EmailForm
# forgive the unused code, will clean up later
if request.method == 'POST':
form = EmailForm(data=request.POST)
if form.is_valid():
contact_name = request.POST.get('contact_name', '')
contact_email = request.POST.get('contact_email', '')
form_content = request.POST.get('content', '')
template = get_template('contact_template.txt')
context =
Context({'contact_name':contact_name,'contact_email':contact_email,'form_content':form_content,})
content = template.render(context)
send_mail('Website Email from ' +
contact_name,content,contact_email, ['[email protected]'],
fail_silently=False)
messages.success(request,'Email was successful!')
else:
messages.error = messages.error(request, 'Fix the error')
return render(request, 'contact.html', {'form': form_class, })
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/5a2531f3-ff59-4061-9cf2-7645c678c8e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.