django-simple-captcha is django's CAPTCHA package, very simple and practical, this record is how to click on the CAPTCHA to refresh the CAPTCHA, because the official document does not give details of this feature.
Official documentation for django-simple-captcha:/en/latest/
The github URL for django-simple-captcha:/mbi/django-simple-captcha
commencement
1.mounting pip install django-simple-captcha, pip install Pillow
2. Add captcha to INSTALLED_APPS.
3. Run python makemigrations and python migrate.
Route joined urlpatterns
urlpatterns = [ path('captcha/', include('')), # image captcha routing path('refresh_captcha/', views.refresh_captcha), # Refresh CAPTCHA, ajax path('test/',IndexView.as_view()), #get and post request paths ]
5. Add the following code to
from import render from import View from import CaptchaStore from import captcha_image_url from import HttpResponse import json # Create CAPTCHA def captcha(): hashkey = CaptchaStore.generate_key() # Captcha answers image_url = captcha_image_url(hashkey) # Captcha address captcha = {'hashkey': hashkey, 'image_url': image_url} return captcha #Refresh CAPTCHA def refresh_captcha(request): return HttpResponse((captcha()), content_type='application/json') # Verify the CAPTCHA def jarge_captcha(captchaStr, captchaHashkey): if captchaStr and captchaHashkey: try: # Get the response value in the database based on the hashkey get_captcha = (hashkey=captchaHashkey) if get_captcha.response == (): # If the captcha matches return True except: return False else: return False class IndexView(View): def get(self, request): hashkey = CaptchaStore.generate_key() # Captcha answers image_url = captcha_image_url(hashkey) # Captcha address print(hashkey,image_url) captcha = {'hashkey': hashkey, 'image_url': image_url} return render(request, "", locals()) def post(self, request): capt = ("captcha", None) # User-submitted authentication codes key = ("hashkey", None) # Captcha answers if jarge_captcha(capt, key): return HttpResponse("Authentication code is correct.") else: return HttpResponse("Captcha error.")
Contents of the folder
{% load static %} <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="/ajax/libs/jquery/3.5.1/"></script> <script src="/ajax/libs/twitter-bootstrap/4.0.0/js/"></script> </head> <body> <form action="/test/" method="post"> {% csrf_token %} <a href="#" rel="external nofollow" class="captcha"> <img src="{{ captcha.image_url }}" alt="Click to switch." > </a> <br> <input type="text" name="captcha" placeholder="CAPTCHA."> <br> <input value="{{ }}" name="hashkey" type="hidden" > <button type="submit" class="btn btn-primary btn-block ">submit (a report etc)</button> </form> <script> <!-- Dynamic Refresh CAPTCHAjs --> $(document).ready(function(){ $('.captcha').click(function () { $.getJSON("/refresh_captcha/", function (result) { $('#id_captcha').attr('src', result['image_url']); $('#id_captcha_0').val(result['hashkey']) }); }); }); </script> </body> </html>
django-simple-captcha doesn't use session for captcha storage, it uses a database, and when you're doing a database migration you'll generate a table captcha_captchastore containing the following fields
challenge = (blank=False, max_length=32) # CAPTCHA capitalization or math calculation like 1+1
response = (blank=False, max_length=32) # CAPTCHA to be entered CAPTCHA in lowercase or result of math calculation e.g. 2
hashkey = (blank=False, max_length=40, unique=True) # hash value
expiration = (blank=False) # expiration time
This is the whole content of this article.