which checks to see if the email is already in the database , however it's never raised on submit.
When submit is called, the view runs and I get the error "The view customer.views.send_email didn't return an HttpResponse object.
It returned None instead", because it's not being passed an actual email. .
I don't understand why the forms.ValidationError isn't stopping it from being submitted? The query in the "clean_email" works fine, so that's not the problem.
I've used this same code before with no problems. I'm sure it's something easy I'm forgetting or missing, but any help is GREATLY appreciated. .
Note: I am using django crispy forms
#Model:
class Customer(models.Model):
email = models.EmailField(max_length=70,blank=False)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('email',)
def __unicode__(self):
return self.email
#Form:
class ContactForm(forms.ModelForm):
class Meta:
model = Customer
fields = ['email']
def clean_email(self):
email = self.cleaned_data['email']
cust_email = Customer.objects.filter(email=email).count()
if cust_email:
raise forms.ValidationError('This email is already in use.')
return email
#View:
def send_email(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
email = cd['email']
new_email = form.save(commit=True)
to_email = form.cleaned_data['email'] # cd['email']
subject = 'Newsletter'
from_email = settings.EMAIL_HOST_USER
message = 'You Are Now Signed Up For BenGui Newsletter!'
#send_mail(subject, message, from_email, [to_email,], fail_silently=False)
return redirect('home')
else:
return render(request, 'home.html', context)
#customer.urls:
urlpatterns = [
url(r'^send-email/$', views.send_email, name='send_email'),
]
#Template:
<form action="{% url 'customer:send_email' %}" method="post">
{% csrf_token %}
{{ form|crispy }}
<input class='btn btn-success btn-lg btn-block' type='submit' value='Submit'></input>
</form>