python - Having issues getting django-simple-captcha to work on my Contact Us page - Stack Overflow
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,
'[email protected]', # Send from your website
['[email protected]'], # 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</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?
最新文章
- 拍照比“剪刀手”注意了!专家:会泄露指纹信息
- 软件定义:英特尔携VMware重塑数据中心
- 评论:Android - 谷歌的一剂药-搜狐滚动
- mip sdk - Can you use the mip sdk to apply a mpip sensitivity label to a .json? - Stack Overflow
- I want to run simple php with frankenphp inside docker - Stack Overflow
- wordpress - Give access to users own submitted entries only wpforms - Stack Overflow
- darkmode - Eclipse IDE - Dark Mode: When "Link With Editor" is enabled in project explorer the selected file&a
- sql - Rolling sum that resets at a flag - Stack Overflow
- python - Multi-level list with each level number included - Stack Overflow
- c# - Having trouble getting the correct position to draw my sprite to when rotating it - Stack Overflow
- javascript - filter out object in array if object key value is null - Stack Overflow
- android - How call Authenticator.authenticate with custom respond in okhttp? - Stack Overflow
- python - Change one pair of vertices to create a cycle in an oriented graph - Stack Overflow
- excel - VBA script to remove duplicates when the gap values between duplicate pair meets certain criteria - Stack Overflow
- python - Using OpenCV to achieve a top-down view of an image with ArUco Markers - Stack Overflow
- Page Performance Issue with Countdown in Angular on Component Reload - Stack Overflow
- python - Convert numpy float to string - Stack Overflow