我正试图在Django
中创建确认页面。我有一个view
,它会form
,然后user
点击Submit
并将对象保存到数据库中。
我想要做的是当用户点击提交时不将对象保存到数据库中。我想渲染另一个页面,用户可以在其中确认订单,这样他就可以看到他将创建的所有对象并单击confirm
。
所以我创建了另一个在提交表单时调用的视图。它应该显示一个包含数据的表,然后,如果用户点击Confirm
,它应该保存对象。问题是 - 因为我正在调用view
并向其发送旧请求,请求为POST
,因此我无法检测到点击提交和旧版重定向之间的区别视图。
所以我可能需要输入参数'确认'到request
我发送到下一个view
。
通常,我会把if request.method == 'POST'
>放在那里使用值
else
渲染表
这是将表单呈现为保存对象的原始视图:
def create_order(request):
import pdb
pdb.set_trace()
LanguageLevelFormSet = formset_factory(LanguageLevelForm, extra=5, max_num=5)
language_level_formset = LanguageLevelFormSet(request.POST or None)
job_creation_form = JobCreationForm(request.POST or None, request.FILES or None)
context = {'job_creation_form': job_creation_form,
'formset': language_level_formset}
if request.method == 'POST':
if job_creation_form.is_valid() and language_level_formset.is_valid():
cleaned_data_job_creation_form = job_creation_form.cleaned_data
cleaned_data_language_level_formset = language_level_formset.cleaned_data
for language_level_form in [d for d in cleaned_data_language_level_formset if d]:
language = language_level_form['language']
level = language_level_form['level']
job = Job(
customer=request.user,
text_to_translate=cleaned_data_job_creation_form['text_to_translate'],
file=cleaned_data_job_creation_form['file'],
short_description=cleaned_data_job_creation_form['short_description'],
notes=cleaned_data_job_creation_form['notes'],
language_from=cleaned_data_job_creation_form['language_from'],
language_to=language,
level=level,
)
job.save()
return HttpResponseRedirect('/order-success')
else:
return render(request, 'auth/jobs/create-job.html', context=context)
return render(request, 'auth/jobs/create-job.html', context=context)
这是新方法:
def create_order(request):
LanguageLevelFormSet = formset_factory(LanguageLevelForm, extra=5, max_num=5)
language_level_formset = LanguageLevelFormSet(request.POST or None)
job_creation_form = JobCreationForm(request.POST or None, request.FILES or None)
context = {'job_creation_form': job_creation_form,
'formset': language_level_formset}
if request.method == 'POST':
if job_creation_form.is_valid() and language_level_formset.is_valid():
cleaned_data_job_creation_form = job_creation_form.cleaned_data
cleaned_data_language_level_formset = language_level_formset.cleaned_data
context = {
'cleaned_data_job_creation_form':cleaned_data_job_creation_form,
"cleaned_data_language_level_formset":cleaned_data_language_level_formset,
}
return confirm_order(request,context)
else:
return render(request, 'auth/jobs/create-job.html', context=context)
return render(request, 'auth/jobs/create-job.html', context=context)
def confirm_order(request,context):
cleaned_data_job_creation_form = context['cleaned_data_job_creation_form']
cleaned_data_language_level_formset = context['cleaned_data_language_level_formset']
if request.method == 'POST': # HERE IS THE PROBLEM, It's POST in both cases
for language_level_form in [d for d in cleaned_data_language_level_formset if d]:
language = language_level_form['language']
level = language_level_form['level']
Job.objects.create(
customer=request.user,
text_to_translate=cleaned_data_job_creation_form['text_to_translate'],
file=cleaned_data_job_creation_form['file'],
short_description=cleaned_data_job_creation_form['short_description'],
notes=cleaned_data_job_creation_form['notes'],
language_from=cleaned_data_job_creation_form['language_from'],
language_to=language,
level=level,
)
return HttpResponseRedirect('/order-success')
file = cleaned_data_job_creation_form['file']
try:
count = utilities.docx_get_characters_number(file)
except utilities.UnknownCharactersNumberException:
count = 0
jobs = []
for language_level_form in [d for d in cleaned_data_language_level_formset if d]:
language = language_level_form['language']
level = language_level_form['level']
d={}
d['language_from']=cleaned_data_job_creation_form['language_from'].name
d['language_to']=language
d['number_of_characters']=count
d['price_per_sign']=LanguageLevel.objects.get(language=language,level=level).price_multiplier*AdminContact.objects.all()[0].price_per_sign
d['estimated_price']=count*d['price_per_sign']
jobs.append(d)
table = CreatedOrdersTable(jobs)
context = {'table':table}
return render(request, 'auth/jobs/confirm-order.html', context=context)
答案 0 :(得分:0)
有两种方法可以做到这一点:
(1)使用"分段"用于存储待处理作业订单状态的表/模型,而不采取任何实际操作来处理订单。这使用户能够回到他们的会话或者稍后向用户发送有关正在进行的作业顺序的提醒等。如果您选择这样做,请遵循标准指南以保存到临时模型对象并返回重定向到视图,该视图将显示包含此登台表输出的确认页面。提交最终订单后,您可以更新实际表并删除登台表条目。
(2)如果您不想使用上述方法,请考虑使用多步骤表单功能来使用会话后端来存储临时状态并将其转发到下一页,直到最终提交。如果您不想手动执行此操作,请查看django-formtools
答案 1 :(得分:0)
我建议的两个可能的选择是