[英]Django form wizard - redirect to first step after done based on a condition
我是Django的新手。 這是我的問題。 我有3個步驟的表單向導。 在第3步結束時,我需要處理數據(通過調用API)。 如果調用的結果為True,則需要將用戶重定向到顯示該向導所有步驟的數據頁面(概述)。 如果結果為False,那么我需要將用戶重定向到顯示消息的頁面...類似“ API調用失敗,您需要再次重新輸入信息”並添加一個用於重定向用戶的按鈕進入向導的第一步,在其中具有所有先前輸入的數據。
當結果為TRUE時,我可以在頁面中顯示摘要。 但是我不知道如何顯示一個頁面,該頁面允許用戶返回到表單的第1步,而先前輸入的數據保持不變。
對於這三種形式,我都有一個模板。
請注意,我簡化了代碼以使其簡短。
forms.py
--------
class Step1Form(forms.Form): def init(self, *args, **kwargs):
super().init(*args, **kwargs) questions =
Question.objects.filter(object_type_id=1)
self = add_form_fields(self,questions) ##This is my method where I am adding fields based on some logic
class Step2Form(forms.Form): def init(self, *args, **kwargs):
super().init(*args, **kwargs)
questions = Question.objects.filter(object_type_id=10)
self = add_form_fields(self,questions)
class Step3Form(forms.Form): def init(self, *args, **kwargs):
super().init(*args, **kwargs)
questions = Question.objects.filter(object_type_id=20)
self = add_form_fields(self,questions)
views.py
--------
class MyQuestionWizard(SessionWizardView):
template_name = 'question.html'
def get(self, request, *args, **kwargs):
try:
return self.render(self.get_form())
except KeyError:
return super().get(request, *args, **kwargs)
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
return render_to_response('done.html',{'form_data': form_data})
question.html (One template for all forms)
-------------------------------------------
{% extends "base.html" %}
{% load i18n %}
{% load widget_tweaks %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}
<form action="" method="post" class="f1" novalidate>{% csrf_token %}
<h3>Provide context about your data</h3>
<p>Complete the required and optional questions below</p>
{{ wizard.management_form }}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group" align="left">
{% if field.field.required %}
<label for="{{ field.auto_id }}" class="required">{{ field.label }}</label>
{% else %}
<label for="{{ field.auto_id }}">{{ field.label }}</label>
{% endif %}
{% render_field field %}
{% if field.help_text %}
<label class="help-text">{{ field.help_text }}</label>
{% endif %}
</div>
{% endfor %}
<br>
{% if wizard.steps.current == wizard.steps.last %}
<div class="f1-buttons">
<button name="wizard_goto_step" type="submit" class="btn btn-previous" value="{{ wizard.steps.prev }}">{% trans "Previous" %}</button>
<button name="wizard_goto_step" type="submit" class="btn btn-submit" value="submit">{% trans "Submit" %}</button>
</div>
{% elif wizard.steps.prev %}
<div class="f1-buttons">
<button name="wizard_goto_step" type="submit" class="btn btn-previous" value="{{ wizard.steps.prev }}">{% trans "Previous" %}</button>
<button name="wizard_goto_step" type="submit" class="btn btn-next" value="{% trans "submit" %}">{% trans "Next" %}</button>
</div>
{% else %}
<div class="f1-buttons">
<button name="wizard_goto_step" type="submit" class="btn btn-next" value="{% trans "submit" %}">{% trans "Next" %}</button>
</div>
{% endif %}
<p align="center"> Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }} </p>
</form>
{% endblock %}
urls.py
---------
from .views import MyQuestionWizard
from django.conf.urls import url, include
from .forms import Step1Form, Step2Form, Step3Form
from django.contrib import admin
urlpatterns = [
#path('', views.introView),
url(r'^myquestion/', MyQuestionWizard.as_view([("step1", Step1Form),
("step2", Step2Form),
("step3", Step3Form)])),
]
這將是結果頁面(向導中未包括)。 我希望此頁面顯示:
1)如果處理數據的結果為TRUE:數據的匯總視圖
2)如果處理數據的結果為FALSE:我想向用戶顯示一條消息,以更新其條目並顯示一個按鈕,該按鈕會將用戶重定向到向導的第一步,而所有先前輸入的數據均保持不變。
I don't know how to do this.
如果API調用為False
,那么您需要在模板上下文中使用經過驗證的表單重新加載向導模板。 像這樣:
class MyQuestionWizard(SessionWizardView):
template_name = 'question.html'
def get(self, request, *args, **kwargs):
try:
return self.render(self.get_form())
except KeyError:
return super().get(request, *args, **kwargs)
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
# If the form is not valid
# Your template should reload the page setting the form already filled
return render_to_response('question.html', {
'form_data': [form.cleaned_data for form in form_list],
})
# If the form is valid
return render_to_response('done.html',{'form_data': form_data})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.