我有一个带有条件额外步骤的SessionWizardView
进程,在第一步结束时基本上会问“你想添加另一个人”,所以通过检查前面的清理数据来生成我的条件步骤;
def function_factory(prev_step):
""" Creates the functions for the condition dict controlling the additional
entrant steps in the process.
:param prev_step: step in the signup process to check
:type prev_step: unicode
:return: additional_entrant()
:rtype:
"""
def additional_entrant(wizard):
"""
Checks the cleaned_data for the previous step to see if another entrant
needs to be added
"""
# try to get the cleaned data of prev_step
cleaned_data = wizard.get_cleaned_data_for_step(prev_step) or {}
# check if the field ``add_another_person`` was checked.
return cleaned_data.get(u'add_another_person', False)
return additional_entrant
def make_condition_stuff(extra_steps, last_step_before_repeat):
cond_funcs = {}
cond_dict = {}
form_lst = [
(u"main_entrant", EntrantForm),
]
for x in range(last_step_before_repeat, extra_steps):
key1 = u"{}_{}".format(ADDITIONAL_STEP_NAME, x)
if x == 1:
prev_step = u"main_entrant"
else:
prev_step = u"{}_{}".format(ADDITIONAL_STEP_NAME, x-1)
cond_funcs[key1] = function_factory(prev_step)
cond_dict[key1] = cond_funcs[key1]
form_lst.append(
(key1, AdditionalEntrantForm)
)
form_lst.append(
(u"terms", TermsForm)
)
return cond_funcs, cond_dict, form_lst
last_step_before_extras = 1
extra_steps = settings.ADDITIONAL_ENTRANTS
cond_funcs, cond_dict, form_list = make_condition_stuff(
extra_steps,
last_step_before_extras
)
我还有一个字典,用于存储通过会话cookie访问的密钥后面的步骤数据,该会话cookie还包含用户输入的人员详细信息列表。在第一个表单之后,此列表呈现为选择框& on selection触发对带有kwargs的SessionWizard
的Ajax调用,该调用触发对返回JsonResponse
的方法的调用;
class SignupWizard(SessionWizardView):
template_name = 'entrant/wizard_form.html'
form_list = form_list
condition_dict = cond_dict
model = Entrant
main_entrant = None
data_dict = dict()
def get_data(self, source_step, step):
session_data_dict = self.get_session_data_dict()
try:
data = session_data_dict[source_step].copy()
data['event'] = self.current_event.id
for key in data.iterkeys():
if step not in key:
newkey = u'{}-{}'.format(step, key)
data[newkey] = data[key]
del data[key]
except (KeyError, RuntimeError):
data = dict()
data['error'] = (
u'There was a problem retrieving the data you requested. '
u'Please resubmit the form if you would like to try again.'
)
response = JsonResponse(data)
return response
def dispatch(self, request, *args, **kwargs):
response = super(SignupWizard, self).dispatch(
request, *args, **kwargs
)
if 'get_data' in kwargs:
data_id = kwargs['get_data']
step = kwargs['step']
response = self.get_data(data_id, step)
# update the response (e.g. adding cookies)
self.storage.update_response(response)
return response
def process_step(self, form):
form_data = self.get_form_step_data(form)
current_step = self.storage.current_step or ''
session_data_dict = self.get_session_data_dict()
if current_step in session_data_dict:
# Always replace the existing data for a step.
session_data_dict.pop(current_step)
if not isinstance(form, TermsForm):
entrant_data = dict()
fields_to_remove = [
'email', 'confirm_email', 'password',
'confirm_password', 'csrfmiddlewaretoken'
]
for k, v in form_data.iteritems():
entrant_data[k] = v
for field in fields_to_remove:
if '{}-{}'.format(current_step, field) in entrant_data:
entrant_data.pop('{}-{}'.format(current_step, field))
if '{}'.format(field) in entrant_data:
entrant_data.pop('{}'.format(field))
for k in entrant_data.iterkeys():
new_key = re.sub('{}-'.format(current_step), u'', k)
entrant_data[new_key] = entrant_data.pop(k)
session_data_dict[current_step] = entrant_data
done = False
for i, data in enumerate(session_data_dict['data_list']):
if data[0] == current_step:
session_data_dict['data_list'][i] = (
current_step, u'{} {}'.format(
entrant_data['first_name'],
entrant_data['last_name']
)
)
done = True
if not done:
session_data_dict['data_list'].append(
(
current_step, u'{} {}'.format(
entrant_data['first_name'],
entrant_data['last_name']
)
)
)
return form_data
如果在不触发Ajax调用的情况下单步执行表单,则表单提交并且条件dict按预期运行。但是,如果触发Ajax并将数据返回到表单,则一旦提交表单,会话数据就会消失。有没有办法改变我的设置方式,以便get_data()
可以将数据返回到页面,而不会破坏会话?
我在开发服务器上将SESSION_ENGINE
设置为cached_db
但是我遇到了一个问题,即当您提交第一个条件表单并且系统调用get_next_step()
后跟get_form_list()
并且条件检查不再返回第一个条件表单,因此我将保留默认表单列表并引发ValueError
,因为current_step
不再是form_list
的一部分
因此,回顾一下,我逐步浏览我的第一个表单,使用'add_another_person'字段触发第一个条件表单,该字段按预期呈现表单,此时form_list
看起来像这样;
form_list
u'main_entrant' <class 'online_entry.forms.EntrantForm'>
u'additional_entrant_1' <class 'online_entry.forms.EntrantForm'>
u'terms' <class 'online_entry.forms.TermsForm'>
但是只要additional_entrant_1
触发Ajax方法,然后提交,form_list
就会运行条件dict&amp;看起来像这样;
form_list
u'main_entrant' <class 'online_entry.forms.EntrantForm'>
u'terms' <class 'online_entry.forms.TermsForm'>
这可能是会话存储或会话无效的问题吗?
答案 0 :(得分:1)
我总是忽略这个简单的解释。
get()
的{{1}}请求重置存储,我正在制作的Ajax调用作为get请求命中视图,重置存储,但也传回我的信息。
因此,通过简单地覆盖SessionWizardView
方法,我已经解决了这个问题;
get()