From 7d245700b036fd92fbf8d3ffc546e93e1ca2cc29 Mon Sep 17 00:00:00 2001 From: Andrea Briganti Date: Fri, 17 Aug 2018 18:39:13 +0200 Subject: [PATCH] you can choose the container id of the widget now --- README.md | 16 ++++++++++++++-- snowpenguin/django/recaptcha2/widgets.py | 12 ++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a710495..a4b104a 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,8 @@ You can pass some parameters into the widget contructor: ```python class ReCaptchaWidget(Widget): - def __init__(self, explicit=False, theme=None, type=None, size=None, tabindex=None, callback=None, - expired_callback=None, attrs={}, *args, **kwargs): + def __init__(self, explicit=False, container_id=None, theme=None, type=None, size=None, tabindex=None, + callback=None, expired_callback=None, attrs={}, *args, **kwargs): ``` If you set the explicit boolean to true, you will render this field with explicit render support. This is useful if you @@ -62,6 +62,18 @@ You can personalize reCaptcha theme, type, size, tabindex, callback and expired_ documentation if you want to change those values. Warning: the app doesn't validate the incoming parameter values. +### Recaptcha "container id" +Now the default container id for the recaptcha is: + +* recaptcha-{$fieldname} for the automatic rendering +* recaptcha-{$fieldname}-{%fiverandomdigits} for the explicit rendering + +This avoids name collisions when you use multiple instances of the recaptcha in different forms, but in the same page +and with the same field name. + +**Note:** you can always override the container id with the "container_id" argument in the widget constructor, but take +care: nobody will check if the id you provide is already used. + ### Templating You can use some template tags to simplify the reCaptcha adoption: diff --git a/snowpenguin/django/recaptcha2/widgets.py b/snowpenguin/django/recaptcha2/widgets.py index efb4603..2dae705 100644 --- a/snowpenguin/django/recaptcha2/widgets.py +++ b/snowpenguin/django/recaptcha2/widgets.py @@ -7,9 +7,10 @@ class ReCaptchaWidget(Widget): - def __init__(self, explicit=False, theme=None, type=None, size=None, tabindex=None, callback=None, - expired_callback=None, public_key=None, attrs={}, *args, **kwargs): + def __init__(self, explicit=False, container_id=None, theme=None, type=None, size=None, tabindex=None, + callback=None, expired_callback=None, public_key=None, attrs={}, *args, **kwargs): super(ReCaptchaWidget, self).__init__(*args, **kwargs) + self.container_id = container_id self.explicit = explicit self.theme = theme self.type = type @@ -24,8 +25,11 @@ def render(self, name, value, attrs=None, *args, **kwargs): template = 'snowpenguin/recaptcha/' template += 'recaptcha_explicit.html' if self.explicit else 'recaptcha_automatic.html' - # this avoids name collisions when you use multiple recaptcha in the same page with the same field name - container_id = 'recaptcha-%s-%s' % (name, randint(10000, 99999)) if self.explicit else 'recaptcha-%s' % name + if self.container_id: + container_id = self.container_id + else: + # this avoids name collisions when you use multiple recaptcha in the same page with the same field name + container_id = 'recaptcha-%s-%s' % (name, randint(10000, 99999)) if self.explicit else 'recaptcha-%s' % name return mark_safe( render_to_string(template, {