Evgenii Legotckoi
Oct. 27, 2019, 2:56 p.m.

EVILEG-CORE. Using Google reCAPTCHA

This will be the first article on the use of a set of established utilities that are present in the core of the EVILEG site. This part of the site’s program code is in open source, so I want to talk about what functionality is present in this battery and how to use it.

I'll start by using Google reCAPTCHA , and what is required to quickly implement this functionality.


Register reCAPTCHA

The first step is to register the site in the admin reCAPTCHA .

An example of registering a site on Google reCAPTCHA

Implementation

Adding Google reCAPTCHA will consist of two steps:

  1. Implementing reCAPTCHA on the client side, i.e. in HTML code
  2. Внедрение reCAPTCHA на серверной стороне

When registering a site, two keys will be generated:

  1. Site key - The key to be placed on the HTML page
  2. Secret key - The key that will be used to communicate the site and the Google service reCAPTCHA

The implementation process will be as follows. On the HTML page in a form that requires validation of user actions, place the reCAPTCHA script and the site key.

On the server side, we add a decorator to the request method for Class Based View.

Install EVILEG-CORE

  1. pip install evileg-core

Also evileg_core will pull up all the dependencies necessary for this package. Including the requests library, which is used to execute the captcha verification request.

settings.py

Add evileg_core to installed applications

  1. INSTALLED_APPS = [
  2. ...
  3. 'evileg_core',
  4. ]

We also add reCAPTCHA Secret Key and Site Key.

  1. GOOGLE_RECAPTCHA_SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  2. GOOGLE_RECAPTCHA_SITE_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

urls.py

We wrap the necessary view with the decorator

  1. # -*- coding: utf-8 -*-
  2.  
  3. from django.conf.urls import url
  4. from evileg_core.decorators import recaptcha
  5.  
  6. from . import views
  7.  
  8. app_name = 'registration'
  9. urlpatterns = [
  10. path('register/', recaptcha(views.RegisterView.as_view()), name='register'),
  11. ]

views.py

And in the view itself, we check the validity of reCAPTCHA. In my case, this was a view inherited from the FormView class.

  1. class RegisterView(FormView):
  2. form_class = UserCreationForm
  3. template_name = 'register.html'
  4.  
  5. def form_valid(self, form):
  6. # reCAPTCHA validation check
  7. if self.request.recaptcha_is_valid:
  8. form.save()
  9. return render(self.request, 'register_success.html', self.get_context_data())
  10. return render(self.request, 'register.html', self.get_context_data())

Client side

On the client side, you need to add the script connection code, the location of the captcha, as well as the output of error messages.

  1. {% extends 'home/base.html' %}
  2. {% block content %}
  3. <form action="{% url 'registration:register' %}" method="post">
  4. {% csrf_token %}
  5. {{ form.as_p }}
  6. {% load recaptcha from evileg_core %}
  7. {% recaptcha 'form-group' %}
  8. {% if messages %}
  9. {% for message in messages %}
  10. {{ message }}
  11. {% endfor %}
  12. {% endif %}
  13. <input type="submit" value="Register">
  14. </form>
  15. {% endblock %}

Result

As a result, we get a fairly simple implementation of reCAPTCHA on the Django site.

Google reCaptcha example on EVILEG-CORE

For Django I recommend Timeweb Hosting VDS Server .

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    April 16, 2025, 5:08 p.m.
    Благодарю за отзыв. И вам желаю всяческих успехов!
  • IscanderChe
    April 12, 2025, 5:12 p.m.
    Добрый день. Спасибо Вам за этот проект и отдельно за ответы на форуме, которые мне очень помогли в некоммерческих пет-проектах. Профессиональным программистом я так и не стал, но узнал мно…
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…