Private
Public Access
1
0

improved contact form

This commit is contained in:
Sander Roosendaal
2020-01-06 22:35:58 +01:00
parent 028ddfcfe7
commit 4c1f717be5
5 changed files with 145 additions and 115 deletions

View File

@@ -115,7 +115,6 @@ class EmailForm(forms.Form):
lastname = forms.CharField(max_length=255) lastname = forms.CharField(max_length=255)
email = forms.EmailField() email = forms.EmailField()
subject = forms.CharField(max_length=255) subject = forms.CharField(max_length=255)
botcheck = forms.CharField(max_length=5)
message = forms.CharField() message = forms.CharField()
disqualificationreasons = ( disqualificationreasons = (

View File

@@ -3,6 +3,7 @@
{% block main %} {% block main %}
<h1>Contact us through email</h1> <h1>Contact us through email</h1>
<ul class="main-content"> <ul class="main-content">
<li class="grid_2"> <li class="grid_2">
{% if form.errors %} {% if form.errors %}
@@ -12,43 +13,40 @@
{% endif %} {% endif %}
<form method="post" action="/rowers/email/send/">{% csrf_token %} <form method="post" action="/rowers/email/send/" id="contactform">{% csrf_token %}
<p>
<table> <table>
<tr><td> <tr><td>
<label class="label">Name <span class="required">*</span></label> <label>Name <span class="required">*</span></label>
<span class="span"> <span class="span">
</td><td> </td><td>
<input name= "firstname" class="inputtext" maxlength="255" size="12" /> <input name= "firstname" class="inputtext" maxlength="255" size="12" />
<label class="spanlabel">First</label> <label>First</label>
</td></tr> </td></tr>
<tr><td> <tr><td>
</span> </span>
<span class="span"> <span class="span">
</td><td> </td><td>
<input name= "lastname" class="inputtext" maxlength="255" size="18" /> <input name= "lastname" class="inputtext" maxlength="255" size="18" />
<label class="spanlabel">Last</label> <label>Last</label>
</span> </span>
</td></tr> </td></tr>
<tr><td> <tr><td>
<label class="label">Email Address <span class="required">*</span></label> <label>Email Address <span class="required">*</span></label>
</td><td> </td><td>
<input name="email" class="inputtext" type="text" maxlength="255" size="35" /> <input name="email" class="inputtext" type="text" maxlength="255" size="35" />
</td></tr> </td></tr>
<tr><td> <tr><td>
<label class="label">Subject <span class="required">*</span></label> <label>Subject <span class="required">*</span></label>
</td><td> </td><td>
<input name="subject" class="inputtext" type="text" maxlength="255" size="45" /> <input name="subject" class="inputtext" type="text" maxlength="255" size="45" />
</td></tr> </td></tr>
</table> </table>
<label class="label">You must answer <u>YES</u> to the question below to approve sending this email. <span class="required">*</span></label>
<table> <table>
<tr><td> <input type="hidden" name="g-recaptcha-response" id='recaptcha'>
Do you want to send me an email?
</td><td>
<input name="botcheck" class="inputtext" type="text" maxlength="5" size="5" />
</td></tr> </td></tr>
<tr><td> <tr><td>
<label class="label">Message <span class="required">*</span></label> <label>Message <span class="required">*</span></label>
</td><td> </td><td>
<textarea name="message" class="inputtextarea" rows="11" cols="45"></textarea> <textarea name="message" class="inputtextarea" rows="11" cols="45"></textarea>
</td></tr> </td></tr>
@@ -56,6 +54,7 @@
<input type="submit" name="submitform" value="Send Message" /> <input type="submit" name="submitform" value="Send Message" />
</td></tr> </td></tr>
</table> </table>
</p>
</form> </form>
</li> </li>
@@ -114,4 +113,24 @@
{% include 'menu_help.html' %} {% include 'menu_help.html' %}
{% endblock %} {% endblock %}
{% block scripts %}
<script src="https://www.google.com/recaptcha/api.js?render=6LdRtMwUAAAAAGcKcFc28pGvmEb1wwDY27i0AX8B"></script>
<script>
// 3
grecaptcha.ready(function() {
// 4
$('#contactform').submit(function(e){
var form = this;
// 5
e.preventDefault()
grecaptcha.execute('6LdRtMwUAAAAAGcKcFc28pGvmEb1wwDY27i0AX8B', {action: 'contactform'}).then(function(token) {
// 6
$('#recaptcha').val(token)
// 7
form.submit()
});
})
});
</script>
{% endblock %}

View File

@@ -1073,28 +1073,39 @@ def add_defaultfavorites(r):
f.save() f.save()
return 1 return 1
# Shows email form and sends it if submitted # Shows email form and sends it if submitted
def sendmail(request): def sendmail(request):
if request.method == 'POST': if request.method == 'POST':
# test recaptcha
response_string = request.POST.get('g-recaptcha-response')
# replace below with settings
recaptcha_secret = '6LdRtMwUAAAAABc3piLDlI5VNDkOtEMIOckNi9tm'
url = 'https://www.google.com/recaptcha/api/siteverify'
data = {
'secret':recaptcha_secret,
'response': response_string,
}
response = requests.post(url,data=data,verify=True)
success = False
if response.status_code == 200:
success = response.json().get('success')
form = EmailForm(request.POST) form = EmailForm(request.POST)
if form.is_valid(): if form.is_valid() and success:
firstname = form.cleaned_data['firstname'] firstname = form.cleaned_data['firstname']
lastname = form.cleaned_data['lastname'] lastname = form.cleaned_data['lastname']
email = form.cleaned_data['email'] email = form.cleaned_data['email']
subject = form.cleaned_data['subject'] subject = 'Rowsandall Contact Form:'+form.cleaned_data['subject']
botcheck = form.cleaned_data['botcheck'].lower()
message = form.cleaned_data['message'] message = form.cleaned_data['message']
if botcheck == 'yes':
try:
fullemail = firstname + " " + lastname + " " + "<" + email + ">" fullemail = firstname + " " + lastname + " " + "<" + email + ">"
send_mail(subject, message, fullemail, ['info@rowsandall.com']) send_mail(subject, message, fullemail, ['info@rowsandall.com'])
return HttpResponseRedirect('/rowers/email/thankyou/') return HttpResponseRedirect('/rowers/email/thankyou/')
except:
return HttpResponseRedirect('/rowers/email/')
else: else:
messages.error(request,'You have to answer YES to the question') if not success:
return HttpResponseRedirect('/rowers/email/') messages.error(request,'Bots are not welcome')
else: else:
messages.error(request,'Something went wrong. Please try again')
return HttpResponseRedirect('/rowers/email/') return HttpResponseRedirect('/rowers/email/')
else: else:
return HttpResponseRedirect('/rowers/email/') return HttpResponseRedirect('/rowers/email/')

View File

@@ -60,7 +60,7 @@ DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG TEMPLATES[0]['OPTIONS']['debug'] = DEBUG
ALLOWED_HOSTS = ['localhost'] ALLOWED_HOSTS = ['localhost','127.0.0.1']
# INSTALLED_APPS += ['debug_toolbar',] # INSTALLED_APPS += ['debug_toolbar',]

View File

@@ -48,6 +48,7 @@
<link rel="stylesheet" href="/static/css/rowsandall2.min.css" /> <link rel="stylesheet" href="/static/css/rowsandall2.min.css" />
{% block meta %} {% endblock %} {% block meta %} {% endblock %}
<div id="fb-root"></div> <div id="fb-root"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>(function(d, s, id) { <script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0]; var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; if (d.getElementById(id)) return;