Skip to content

Commit

Permalink
you can choose the container id of the widget now
Browse files Browse the repository at this point in the history
  • Loading branch information
kbytesys committed Aug 17, 2018
1 parent a79744f commit 7d24570
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -62,6 +62,18 @@ You can personalize reCaptcha theme, type, size, tabindex, callback and expired_
<a href="https://developers.google.com/recaptcha/docs/display#config">documentation</a> 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:

Expand Down
12 changes: 8 additions & 4 deletions snowpenguin/django/recaptcha2/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, {
Expand Down

0 comments on commit 7d24570

Please sign in to comment.