@@ 1,12 1,13 @@
# -*- coding: utf-8 -*-
from django import forms
+from django.conf import settings
from django.core.exceptions import PermissionDenied
from .settings import FORM_GUARD_MIN_VALUE
def clean_func(self):
- antispam = self.cleaned_data.get('antispam')
+ antispam = self.cleaned_data.get(self.FORM_GUARD_FIELD_NAME)
if settings.DEBUG:
return antispam
try:
@@ 23,9 24,10 @@ class FormGuardForm(forms.Form):
def __init__(self, *args, **kwargs):
super(FormGuardForm, self).__init__(*args, **kwargs)
- self.fields[FORM_GUARD_FIELD_NAME] = forms.CharField(
- widget=forms.HiddenInput(),
+ field_id = 'id_{0}'.format(self.FORM_GUARD_FIELD_NAME)
+ self.fields[self.FORM_GUARD_FIELD_NAME] = forms.CharField(
initial='default',
- attrs={'id': 'id_{0}'.format(FORM_GUARD_FIELD_NAME)})
+ widget=forms.HiddenInput(attrs={'id': field_id}),
)
- setattr(self, 'clean_{0}'.format(FORM_GUARD_FIELD_NAME), clean_func)
+ clean_func_name = 'clean_{0}'.format(self.FORM_GUARD_FIELD_NAME)
+ setattr(FormGuardForm, clean_func_name, clean_func)
@@ 1,11 1,10 @@
# -*- coding: utf-8 -*-
from django import template
-from .forms import FormGuardForm
-
register = template.Library()
-@register.inclusion_tag('form_guard/js_snippet.html')
+@register.inclusion_tag('form_guard/js_snippet.html', takes_context=True)
def form_guard_js(context, form):
- return {'FORM_GUARD_FIELD_ID': form.fields[form.FORM_GUARD_FIELD_NAME].id}
+ field_id = form.fields[form.FORM_GUARD_FIELD_NAME].widget.attrs.get('id')
+ return {'FORM_GUARD_FIELD_ID': field_id}