Decorators¶
evileg_core.decorators module¶
Examples¶
Using recaptcha¶
Add recaptcha key to settings.py
GOOGLE_RECAPTCHA_SECRET_KEY = 'your-secret-token'
GOOGLE_RECAPTCHA_SITE_KEY = 'your-site-key'
Add recaptcha to view in urls
from django.conf.urls import url
from evileg_core.decorators import recaptcha
from . import views
app_name = 'registration'
urlpatterns = [
url(r'^register/$', recaptcha(views.RegisterView.as_view()), name='register'),
]
Add recaptcha site key in template with form
<form action="{% url 'register' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
{% load evileg_core %}
{% recaptcha %}
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
<input type="submit" value="Register">
</form>
Check recaptcha in your form view
class RegisterView(FormView):
form_class = UserCreationForm
template_name = 'register.html'
def form_valid(self, form):
if self.request.recaptcha_is_valid:
form.save()
return render(self.request, 'register_success.html', self.get_context_data())
return render(self.request, 'register.html', self.get_context_data())