gpt4 book ai didi

python - Django SessionWizardView 在 Ajax 请求后 form_list 中缺少当前步骤

转载 作者:行者123 更新时间:2023-11-28 18:36:59 24 4
gpt4 key购买 nike

我有一个带有条件额外步骤的 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
)

我还有一个字典,它在可通过 session cookie 访问的 key 后面存储步骤数据,该 key 还包含用户输入的人员详细信息列表。在第一个表单之后,此列表呈现为一个选择框,并且在选择时触发对带有 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 调用的情况下单步执行表单,则表单会提交并且条件字典会按预期运行。但是如果 Ajax 被触发并且数据返回到表单,一旦您提交表单, session 数据似乎已经消失。有没有一种方法可以改变我获得此设置的方式,以便 get_data() 可以将数据返回到页面,而不会破坏 session ?

我在开发服务器上将 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 就会运行条件字典 & 看起来像这样;

form_list   
u'main_entrant' <class 'online_entry.forms.EntrantForm'>
u'terms' <class 'online_entry.forms.TermsForm'>

这可能是 session 存储或 session 失效的问题吗?

最佳答案

我总是忽略简单的解释。

SessionWizardViewget() 请求重置了存储,而我进行的 Ajax 调用作为 get 请求命中了 View ,重置了存储,而且传回我的信息。

因此,通过简单覆盖 get() 方法,我解决了这个问题;

def get(self, request, *args, **kwargs):
if 'get_data' in kwargs:
data_id = kwargs['get_data']
step = kwargs['step']
response = self.get_data(data_id, step)
else:
self.storage.reset()

# reset the current step to the first step.
self.storage.current_step = self.steps.first
response = self.render(self.get_form())
return response

关于python - Django SessionWizardView 在 Ajax 请求后 form_list 中缺少当前步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31457408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com