具有动态表单的 Django Formwizard 不会继续下一步
Django Formwizard with dynamic form does not proceed to next step
我正在尝试创建一个 Django 表单向导,其中第一个表单具有在运行时生成的选择字段。我可以使用正确的选项生成表单,但是当我单击提交时,表单向导不会继续执行下一步。它提交表单但 returns 到 form1。这是我的表格和视图
##Forms
class EventForm1(forms.Form):
def __init__(self, *args, **kwargs):
##Get choices at runtime
choices = kwargs.pop('loan_choices', [])
super(EventForm1, self).__init__(*args, **kwargs)
self.fields['loan'] = forms.ChoiceField(choices=choices, required=False, widget=forms.Select(attrs={'class':'events_loan_name', 'event_attribute':'loan', 'name':'loan'}) )
attribute_to_change = forms.ChoiceField(choices=[('monthly_payment','Monthly Payment'), ('interest_rate','Interest Rate')], widget=forms.Select(attrs={'class': 'attribute_to_change', 'name':'attribute_to_change', 'event_attribute':'attribute_to_change'}))
class EventForm2(forms.Form):
start_date = forms.DateField(widget=forms.TextInput(attrs={'class': 'event_dates','start_date':'start_date', 'name':'event_start_date', 'event_attribute':'start_date'}))
##Views
class EventFormWizard(SessionWizardView):
"""Form wizard for adding an event"""
def done(self, form_list, **kwargs):
self.finalize_new_event(form_list)
return HttpResponseRedirect('/')
def get_form(self, step=None, data=None, files=None):
form = super(EventFormWizard, self).get_form(step, data, files)
# determine the step if not given
if step is None:
step = self.steps.current
print(step, form)
if step == "0":
loan_sessions = self.request.session.get('loan_sessions', None)
if loan_sessions != None:
loan_sessions = pickle.loads(self.request.session['loan_sessions'])
else:
loan_sessions = []
loan_choices = []
for loan_session in loan_sessions:
loan_choice = (loan_session['number'], loan_session['name'])
loan_choices.append(loan_choice)
##Update the choices for the select
form = EventForm1(loan_choices=loan_choices)
return form
def finalize_new_loan(self, form_list):
"""Adds a autmatic properties to the new loan, saves it to the loan_sessions and deletes the new loan object from the session"""
for form in form_list:
cleaned_data = form.cleaned_data
print(cleaned_data)
我正在使用带有 form-tools 包的 django 1.8。
我发现发生了什么事。表单向导的 post 方法调用 get_form 如下:
# get the form for the current step
form = self.get_form(data=self.request.POST, files=self.request.FILES)
print(form.is_valid(), form.errors, form.is_bound)
# and try to validate
if form.is_valid():
# if the form is valid, store the cleaned data and files.
self.storage.set_step_data(self.steps.current,
self.process_step(form))
self.storage.set_step_files(self.steps.current,
self.process_step_files(form))
# check if the current step is the last step
if self.steps.current == self.steps.last:
# no more steps, render done view
return self.render_done(form, **kwargs)
else:
# proceed to the next step
return self.render_next_step(form)
return self.render(form)
如果当前表单通过验证,它只会调用下一步的表单,而它永远不会这样做,因为我的自定义 get_form 挂钩在创建动态表单时不会检查 POST 数据。无论如何,这是工作习惯 get_form 挂钩:
def get_form(self, step=None, data=None, files=None):
form = super(EventFormWizard, self).get_form(step, data, files)
# determine the step if not given
if step is None:
step = self.steps.current
if step == "0":
loan_sessions = self.request.session.get('loan_sessions', None)
if loan_sessions != None:
loan_sessions = pickle.loads(self.request.session['loan_sessions'])
else:
loan_sessions = []
loan_choices = []
for loan_session in loan_sessions:
loan_choice = (loan_session['number'], loan_session['name'])
loan_choices.append(loan_choice)
##Pass the data when initing the form, which is the POST
## data if the got_form function called during a post
## or the self.storage.get_step_data(form_key) if the form wizard
## is validating this form again in the render_done method
form = EventForm1(loan_choices=loan_choices, data=data)
return form
我正在尝试创建一个 Django 表单向导,其中第一个表单具有在运行时生成的选择字段。我可以使用正确的选项生成表单,但是当我单击提交时,表单向导不会继续执行下一步。它提交表单但 returns 到 form1。这是我的表格和视图
##Forms
class EventForm1(forms.Form):
def __init__(self, *args, **kwargs):
##Get choices at runtime
choices = kwargs.pop('loan_choices', [])
super(EventForm1, self).__init__(*args, **kwargs)
self.fields['loan'] = forms.ChoiceField(choices=choices, required=False, widget=forms.Select(attrs={'class':'events_loan_name', 'event_attribute':'loan', 'name':'loan'}) )
attribute_to_change = forms.ChoiceField(choices=[('monthly_payment','Monthly Payment'), ('interest_rate','Interest Rate')], widget=forms.Select(attrs={'class': 'attribute_to_change', 'name':'attribute_to_change', 'event_attribute':'attribute_to_change'}))
class EventForm2(forms.Form):
start_date = forms.DateField(widget=forms.TextInput(attrs={'class': 'event_dates','start_date':'start_date', 'name':'event_start_date', 'event_attribute':'start_date'}))
##Views
class EventFormWizard(SessionWizardView):
"""Form wizard for adding an event"""
def done(self, form_list, **kwargs):
self.finalize_new_event(form_list)
return HttpResponseRedirect('/')
def get_form(self, step=None, data=None, files=None):
form = super(EventFormWizard, self).get_form(step, data, files)
# determine the step if not given
if step is None:
step = self.steps.current
print(step, form)
if step == "0":
loan_sessions = self.request.session.get('loan_sessions', None)
if loan_sessions != None:
loan_sessions = pickle.loads(self.request.session['loan_sessions'])
else:
loan_sessions = []
loan_choices = []
for loan_session in loan_sessions:
loan_choice = (loan_session['number'], loan_session['name'])
loan_choices.append(loan_choice)
##Update the choices for the select
form = EventForm1(loan_choices=loan_choices)
return form
def finalize_new_loan(self, form_list):
"""Adds a autmatic properties to the new loan, saves it to the loan_sessions and deletes the new loan object from the session"""
for form in form_list:
cleaned_data = form.cleaned_data
print(cleaned_data)
我正在使用带有 form-tools 包的 django 1.8。
我发现发生了什么事。表单向导的 post 方法调用 get_form 如下:
# get the form for the current step
form = self.get_form(data=self.request.POST, files=self.request.FILES)
print(form.is_valid(), form.errors, form.is_bound)
# and try to validate
if form.is_valid():
# if the form is valid, store the cleaned data and files.
self.storage.set_step_data(self.steps.current,
self.process_step(form))
self.storage.set_step_files(self.steps.current,
self.process_step_files(form))
# check if the current step is the last step
if self.steps.current == self.steps.last:
# no more steps, render done view
return self.render_done(form, **kwargs)
else:
# proceed to the next step
return self.render_next_step(form)
return self.render(form)
如果当前表单通过验证,它只会调用下一步的表单,而它永远不会这样做,因为我的自定义 get_form 挂钩在创建动态表单时不会检查 POST 数据。无论如何,这是工作习惯 get_form 挂钩:
def get_form(self, step=None, data=None, files=None):
form = super(EventFormWizard, self).get_form(step, data, files)
# determine the step if not given
if step is None:
step = self.steps.current
if step == "0":
loan_sessions = self.request.session.get('loan_sessions', None)
if loan_sessions != None:
loan_sessions = pickle.loads(self.request.session['loan_sessions'])
else:
loan_sessions = []
loan_choices = []
for loan_session in loan_sessions:
loan_choice = (loan_session['number'], loan_session['name'])
loan_choices.append(loan_choice)
##Pass the data when initing the form, which is the POST
## data if the got_form function called during a post
## or the self.storage.get_step_data(form_key) if the form wizard
## is validating this form again in the render_done method
form = EventForm1(loan_choices=loan_choices, data=data)
return form