Django表单向导-根据条件重定向到第一步

时间:2019-07-19 00:45:17

标签: python django forms wizard

我是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)])),
]

done.html

这将是结果页面(向导中未包括)。我希望此页面显示:

1)如果处理数据的结果为TRUE:数据的汇总视图

2)如果处理数据的结果为FALSE: 我想向用户显示一条消息,以更新他们的条目 并显示一个将用户重定向到向导第一步的按钮, 保留所有先前输入的数据。


I don't know how to do this.


1 个答案:

答案 0 :(得分:0)

如果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})