Skip to content

Commit 7d24570

Browse files
committed
you can choose the container id of the widget now
1 parent a79744f commit 7d24570

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ You can pass some parameters into the widget contructor:
5151

5252
```python
5353
class ReCaptchaWidget(Widget):
54-
def __init__(self, explicit=False, theme=None, type=None, size=None, tabindex=None, callback=None,
55-
expired_callback=None, attrs={}, *args, **kwargs):
54+
def __init__(self, explicit=False, container_id=None, theme=None, type=None, size=None, tabindex=None,
55+
callback=None, expired_callback=None, attrs={}, *args, **kwargs):
5656
```
5757

5858
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_
6262
<a href="https://developers.google.com/recaptcha/docs/display#config">documentation</a> if you want to change those values.
6363
Warning: the app doesn't validate the incoming parameter values.
6464

65+
### Recaptcha "container id"
66+
Now the default container id for the recaptcha is:
67+
68+
* recaptcha-{$fieldname} for the automatic rendering
69+
* recaptcha-{$fieldname}-{%fiverandomdigits} for the explicit rendering
70+
71+
This avoids name collisions when you use multiple instances of the recaptcha in different forms, but in the same page
72+
and with the same field name.
73+
74+
**Note:** you can always override the container id with the "container_id" argument in the widget constructor, but take
75+
care: nobody will check if the id you provide is already used.
76+
6577
### Templating
6678
You can use some template tags to simplify the reCaptcha adoption:
6779

snowpenguin/django/recaptcha2/widgets.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88

99
class ReCaptchaWidget(Widget):
10-
def __init__(self, explicit=False, theme=None, type=None, size=None, tabindex=None, callback=None,
11-
expired_callback=None, public_key=None, attrs={}, *args, **kwargs):
10+
def __init__(self, explicit=False, container_id=None, theme=None, type=None, size=None, tabindex=None,
11+
callback=None, expired_callback=None, public_key=None, attrs={}, *args, **kwargs):
1212
super(ReCaptchaWidget, self).__init__(*args, **kwargs)
13+
self.container_id = container_id
1314
self.explicit = explicit
1415
self.theme = theme
1516
self.type = type
@@ -24,8 +25,11 @@ def render(self, name, value, attrs=None, *args, **kwargs):
2425
template = 'snowpenguin/recaptcha/'
2526
template += 'recaptcha_explicit.html' if self.explicit else 'recaptcha_automatic.html'
2627

27-
# this avoids name collisions when you use multiple recaptcha in the same page with the same field name
28-
container_id = 'recaptcha-%s-%s' % (name, randint(10000, 99999)) if self.explicit else 'recaptcha-%s' % name
28+
if self.container_id:
29+
container_id = self.container_id
30+
else:
31+
# this avoids name collisions when you use multiple recaptcha in the same page with the same field name
32+
container_id = 'recaptcha-%s-%s' % (name, randint(10000, 99999)) if self.explicit else 'recaptcha-%s' % name
2933

3034
return mark_safe(
3135
render_to_string(template, {

0 commit comments

Comments
 (0)