SHARE
TWEET

Untitled

a guest Oct 21st, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Html:
  2.  
  3. <script>
  4.     grecaptcha.ready(function() {
  5.         grecaptcha.execute(<KEY>, {action: ''})
  6.         .then(function(token) {
  7.             document.getElementById("cform").appendChild(document.CreateElement(`<input type="hidden" name="g-recaptcha-response" value=${token}`);
  8.         });
  9.     });
  10. </script>
  11.  
  12. <form id='cform' role="form" action="" method="post">
  13.     {% csrf_token %}
  14.     {{ form.as_p }}
  15.    
  16.     <script src='https://www.google.com/recaptcha/api.js?render=<KEY>'></script>
  17.     <div class="g-recaptcha" data-sitekey="<KEY>"></div>
  18.  
  19.     <button type="submit">Submit</button>
  20. </form>
  21.  
  22.  
  23. Views.py:
  24.  
  25. def contact(request):
  26.     form_class = ContactForm
  27.  
  28.     # new logic!
  29.     if request.method == 'POST':
  30.         form = form_class(data=request.POST)
  31.  
  32.         if form.is_valid():
  33.  
  34.             recaptcha_response = request.POST.get('g-recaptcha-response')
  35.             url = 'https://www.google.com/recaptcha/api/siteverify'
  36.             payload = {
  37.                 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
  38.                 'response': recaptcha_response
  39.             }
  40.             data = urllib.parse.urlencode(payload).encode()
  41.             req = urllib.request.Request(url, data=data)
  42.  
  43.             response = urllib.request.urlopen(req)
  44.             result = json.loads(response.read().decode())
  45.  
  46.             if (not result['success']) or (not result['action'] == ''):
  47.                 messages.error(request, 'Invalid reCAPTCHA. Please try again.')
  48.  
  49.             contact_name = request.POST.get(
  50.                 'contact_name'
  51.             , '')
  52.             contact_email = request.POST.get(
  53.                 'contact_email'
  54.             , '')
  55.             form_content = request.POST.get('content', '')
  56.  
  57.             # Email the profile with the
  58.             # contact information
  59.             template = get_template('contact_template.txt')
  60.             context = {
  61.                 'contact_name': contact_name,
  62.                 'contact_email': contact_email,
  63.                 'form_content': form_content,
  64.             }
  65.             content = template.render(context)
  66.  
  67.             email = EmailMessage(
  68.                 "New contact form submission",
  69.                 content,
  70.                 "Your website" +'',
  71.                 ['GMAIL_EMAIL'],
  72.                 headers = {'Reply-To': contact_email }
  73.             )
  74.             email.send()
  75.             messages.info(request, "Success")
  76.  
  77.     return render(request, 'contact.html', {
  78.         'form': form_class,
  79.     })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top