When django serves a view the form class UserRegisterForm(forms.Form) is used and this form's init function is executed twice.
I'm unable to determine why the form is executed twice.
The RegisterView overrides the get() function provided by FormView. If I comment out the get function, the Form is returned correctly and the init function for UserRegisterForm is only executed once.
class RegisterView(generic.FormView):
template_name = 'dl_user/register.html'
form_class = UserRegisterForm
success_url = reverse_lazy('dl_user:register_success')
def get(self, request):
request.session._get_or_create_session_key()
prices_json = json.dumps(settings.prices, cls=DjangoJSONEncoder)
context = {
'form': self.form_class,
'prices': prices_json,
}
request.session['prices'] = prices_json
return render(request, self.template_name, context)
RegesterView renders UserRegisterForm
class UserRegisterForm(forms.Form):
request = None
payment_method = forms.TypedChoiceField(
choices=payment.getPaymentOptions(),
coerce=lambda x: str(x),
widget=forms.Select,
initial='1',
required=True)
payment_amount = forms.DecimalField(required=True, max_digits=100, decimal_places=8)
# mandatory schema fields during registration
username = forms.CharField(required=True,
min_length=3,
max_length=30,
help_text='Choose a memorable name e.g jdoe',
validators=[UnicodeUsernameValidator()])
password = forms.CharField(widget=forms.PasswordInput, min_length=8)
password1 = forms.CharField(widget=forms.PasswordInput, min_length=8, label='Confirm Password')
def __init__(self, *args, **kwargs):
self.request = kwargs.get('request')
if 'request' in kwargs:
del kwargs['request']
super(UserRegisterForm, self).__init__(*args, **kwargs)
self.ldap_ops = LDAPOperations()
self.helper = FormHelper()
self.helper.form_id = 'id-user-data-form'
self.helper.form_method = 'post'
# self.helper.form_action = 'register'
self.helper.add_input(Submit('submit', 'Submit', css_class='btn-success'))
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-md-2'
self.helper.field_class = 'col-md-8'
self.helper.error_text_inline = False
self.helper.layout = Layout(
Fieldset('Login Details',
'username', 'password', 'password1'),
)
self.helper.layout.append(
Fieldset('Payment',
Field('membership_in_days', onclick="updatePrice(value);"),
Field('payment_method', onclick="updatePaymentMethod(value)"),
Field('payment_amount', readonly=True),
)
)
I expect the form to load correctly, without UserRegisterForm.init being executed twice.