Decorators

evileg_core.decorators module

evileg_core.decorators.non_login_required(function=None, redirect_url='/')[source]

Decorator for views that checks that the user is not logged in, redirecting to the main page or target page by redirect_url if user is logged in.

evileg_core.decorators.recaptcha(function)[source]

Google recaptcha decorator. It can be used against robots and brute force.

Parameters

function – wrapped view function, which should be checked via Google recaptcha

Returns

wrapped function

Examples

Using recaptcha

  1. Add recaptcha key to settings.py

GOOGLE_RECAPTCHA_SECRET_KEY = 'your-secret-token'
GOOGLE_RECAPTCHA_SITE_KEY = 'your-site-key'
  1. 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'),
]
  1. 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>
  1. 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())