django - Change form input attribute 'name' to 'data-encrypted-name' -
this tricky question title, please read before assuming duplicate :).
i'm using braintree payments on django site, , payment form html needs credit card number:
<input type="text" size="20" autocomplete="off" data-encrypted-name="number" />
mine looks this:
<input type="text" size="20" autocomplete="off" name="number">
can somehow rename name
data-encrypted-name
? alternatively, can hide/remove name
attribute altogether? if so, add custom attribute braintree-friendly attribute:
class signupform(forms.form): ...snip... def __init__(self, *args, **kwargs): super(signupform, self).__init__(*args, **kwargs) self.fields['number'].widget.attrs['data-encrypted-name'] = "number"
fyi tried in __init__
no luck:
self.fields['number'].widget.attrs['name'] = none
per braintree:
important: not use name attribute fields capture sensitive payment information such credit card number or cvv. removing attribute prevents them hitting server in plain text , reduces pci compliance scope.
also, i'm using django crispy forms, i'd prefer solve in forms.py
, not in template html tweaks in order keep dry.
define custom widget class inheriting whatever widget type numbers
field defaults (textinput
, judging tag you're showing) , override build_attrs
method.
i'd this:
class sensitivetextinput(textinput): def build_attrs(self, extra_attrs=none, **kwargs): attrs = super(sensitivetextinput, self).build_attrs(extra_attrs, **kwargs) if 'name' in attrs: attrs['data-encrypted-name'] = attrs['name'] del attrs['name'] return attrs
if need more handful of widget types abstract mixin.
Comments
Post a Comment