SHARE
TWEET

Untitled

a guest Oct 21st, 2019 77 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.             ginput = document.createElement('input');
  8.             ginput.type = "hidden";
  9.             ginput.name = "g-recaptcha-response";
  10.             ginput.value = token;
  11.             document.getElementById("cform").appendChild(ginput);
  12.         });
  13.     });
  14. </script>
  15.  
  16. <form id='cform' role="form" action="" method="post">
  17.     {% csrf_token %}
  18.     {{ form.as_p }}
  19.    
  20.     <script src='https://www.google.com/recaptcha/api.js?render=<KEY>'></script>
  21.     <div class="g-recaptcha" data-sitekey=<KEY>></div>
  22.  
  23.     <button type="submit">Submit</button>
  24. </form>
  25.  
  26.  
  27. Views.py:
  28.  
  29. def contact(request):
  30.     form_class = ContactForm
  31.  
  32.     if request.method == 'POST':
  33.         form = form_class(data=request.POST)
  34.  
  35.         if form.is_valid():
  36.  
  37.             recaptcha_response = request.POST.get('g-recaptcha-response')
  38.             url = 'https://www.google.com/recaptcha/api/siteverify'
  39.             payload = {
  40.                 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
  41.                 'response': recaptcha_response
  42.             }
  43.             data = urllib.parse.urlencode(payload).encode()
  44.             req = urllib.request.Request(url, data=data)
  45.  
  46.             response = urllib.request.urlopen(req)
  47.             result = json.loads(response.read().decode())
  48.  
  49.             if (not result['success']) or (not result['action'] == ''):
  50.                 messages.error(request, 'Invalid reCAPTCHA. Please try again.')
  51.  
  52.             contact_name = request.POST.get(
  53.                 'contact_name'
  54.             , '')
  55.             contact_email = request.POST.get(
  56.                 'contact_email'
  57.             , '')
  58.             form_content = request.POST.get('content', '')
  59.  
  60.             # Email the profile with the
  61.             # contact information
  62.             template = get_template('contact_template.txt')
  63.             context = {
  64.                 'contact_name': contact_name,
  65.                 'contact_email': contact_email,
  66.                 'form_content': form_content,
  67.             }
  68.             content = template.render(context)
  69.  
  70.             email = EmailMessage(
  71.                 "New contact form submission",
  72.                 content,
  73.                 "Your website" +'',
  74.                 ['GMAIL_EMAIL'],
  75.                 headers = {'Reply-To': contact_email }
  76.             )
  77.             email.send()
  78.             messages.info(request, "Success")
  79.  
  80.     return render(request, 'contact.html', {
  81.         'form': form_class,
  82.     })
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