Having issues getting django-simple-captcha to work on my Contact Us page
I'm having trouble getting the captcha to work on my Contact Us page in my website. I've followed the instructions shown in the docs but keep getting errors. Can anyone help? Here's my forms.py:
# main/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
# For the Captcha
from captcha.fields import CaptchaField
# For the Contact Us page
from django.core.validators import EmailValidator
class CustomUserCreationForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self):
user = super(UserCreationForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
user.save()
return user
class ContactForm(forms.Form):
name = forms.CharField(required=True)
email = forms.EmailField(required=True)
# phone = forms.CharField(max_length=15)
# subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
captcha = CaptchaField()
Here is my urls.py:
# main/urls.py
from django.contrib import admin
from django.urls import path, include
from main.views import dashboard, register, about, blog
app_name = 'main'
urlpatterns = [
path('blog/', include('blog.urls')),
path('admin/', admin.site.urls),
path('projects/', include('projects.urls')),
path('captcha/', include('captcha.urls')),
]
Here is my views.py:
# main/views.py
from django.contrib.auth import login as auth_login
from django.shortcuts import redirect, render
from django.urls import reverse
from main.forms import CustomUserCreationForm
from captcha.fields import CaptchaField
from django.core.mail import EmailMessage
from main.forms import ContactForm
from django.conf import settings
from django.http import HttpResponse
# Create your views here.
def main(request):
return render(request, 'main.html', {})
def dashboard(request):
return render(request, 'users/dashboard.html')
def admin(request):
return render(request, 'admin')
def login(request):
return render(request, 'registration/login.html')
def register(request):
if request.method == 'GET':
return render(
request, 'users/register.html',
{'form': CustomUserCreationForm}
)
elif request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
user = form.save()
auth_login(request, user)
return render(request, 'users/dashboard.html')
else:
return HttpResponse('<h1>Invalid</h1>')
def about(request):
return render(request, 'about.html')
def blog(request):
import pdb; pdb.set_trace()
return render(request, 'blog/blog_index.html')
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# Process the form data
name = form.cleaned_data['name']
email = form.cleaned_data['email']
# subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
human = True
EmailMessage(
'Contact Form Submission from {}'.format(name),
message,
'admin@lennyshort.com', # Send from your website
['admin@lennyshort.com'], # Send to (your admin email)
[],
reply_to=[email] # Email from the form to get back to
).send()
return redirect('success')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
def success(request):
return HttpResponse('Success!<p>Return to <a href="#">my_domain.com</a>.')
And here is my contact.html:
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form action="/contact/" method="post">
{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
{ form.captcha }}
<input type="submit" value="Submit">
</form>
</body>
</html>
I've tried moving things around in the code several different ways, but I can't seem to get it to work and I cannot figure out why. Can anyone help me with this?