📅  最后修改于: 2023-12-03 14:40:46.536000             🧑  作者: Mango
Django Recapcha is a Python package that provides an easy way to add Google recapcha to Django forms. Google recapcha is a free service that helps protect websites from spam and abuse by requiring users to complete a simple test to prove they are human.
Django Recapcha makes it easy to integrate Google recapcha into your Django forms, helping protect your website and users from spam and abuse.
To install Django Recapcha, run the following command:
pip install django-recaptcha
To use Django Recapcha, first add captcha
to your installed apps in your Django settings file:
INSTALLED_APPS = [
# ...
'captcha',
]
Then, in your Django form, import the ReCaptchaField'
and add it to the form fields:
from captcha.fields import ReCaptchaField
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea)
captcha = ReCaptchaField()
Finally, add the recapcha widget to your form template using the following code:
{% load captcha %}
<form>
<!-- form fields -->
{% captcha %}
<button type="submit">Submit</button>
</form>
Django Recapcha allows you to customize the appearance, theme, and language of the recapcha widget. To do this, add the following settings to your Django settings file:
# Set the recapcha theme (light or dark)
RECAPTCHA_THEME = 'light'
# Set the recapcha language (defaults to English)
RECAPTCHA_LANG = 'en'
# Customize the recapcha widget with CSS classes
RECAPTCHA_WIDGET_CLASS = 'custom-class'
In conclusion, Django Recapcha is a powerful and easy-to-use package that can help protect your website and users from spam and abuse. By adding Google recapcha to your Django forms, you can rest easy knowing that your website is secure and your users are protected.