Advertisement
An0n0ym0usHacker

Views File

Mar 27th, 2021
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. htmly = render_to_string('newsletter_confirm.html')
  2.  
  3. @csrf_exempt
  4. def subscribe(request):
  5.     template_name = 'subscribe.html'
  6.     try:
  7.         if request.method == 'POST':
  8.             sub = Subscriber(
  9.                 email=request.POST['email'], conf_num=random_digits())
  10.             sub.save()
  11.             recent = Blog.objects.all().filter(status="Published").order_by('-created_on')[:3]
  12.             message = Mail(
  13.                 from_email=formataddr((str(Header('Pathway To Light', 'utf-8')), settings.FROM_EMAIL)),
  14.                 #from_email=settings.FROM_EMAIL,
  15.                 to_emails=sub.email,
  16.                 subject='Newsletter Confirmation',
  17.                 html_content=htmly.format(request.build_absolute_uri('/confirm/'), sub.email, sub.conf_num, {'recent': recent }))
  18.             sg = SendGridAPIClient(settings.SENDGRID_API_KEY)
  19.             response = sg.send(message)
  20.  
  21.             return render(request, 'pathway/accept.html')
  22.         else:
  23.             form = SubscriberForm()
  24.  
  25.         return render(request, template_name, {'form': form})
  26.  
  27.     except IntegrityError:
  28.         return HttpResponse('<script> window.alert("Email already Exists...");window.location.href="/"; </script>')
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement