使用基于Django类的CreateView做多步骤表单

时间:2018-06-21 05:16:16

标签: django

我有多个基于类的createview。我的目标是链接所有createview,这样当我发布第一个createview时,它将重定向我到第二个createview,在该位置它将检索从第一个createview输入的数据。

有人知道解决方案吗?

第一个createview(Step01)包含django-inline-formset-factory代码,类似于this代码,而其余的(Step02和Step03)包含基本代码。

我已经提到this link,但是解决方案基于使用基于函数的视图。也曾尝试使用Django的Form-Wizard,但用django-inline-formset进行处理对于我来说仍然太复杂。

我为什么要这样做?这样每个组级别(即总,员工,管理人员)都可以访问各自的视图,但不能访问其他组级别的视图。

这是我当前的代码:

models.py

class Model_Step01_Cart(models.Model):
    cart_name = models.CharField(max_length=100)

class Model_Step01_CartItem(models.Model):
    cart = models.ForeignKey(Profile)
    item_name = models.CharField(max_length = 100)
    item_quantity = models.FloatField(null = True, blank = True)

class Model_Step02_Staffnote(models.Model):
    note_staff = models.TextField(max_length = 500, null = True, blank = True)

class Model_Step03_Managernote(models.Model):
    note_manager = models.TextField(max_length = 500, null = True, blank = True)

forms.py

class Form_Step01_Cart(forms.ModelForm):
    class Meta:
        model = Model_Step01_Cart
    fields = ["cart_name"]

class Form_Step01_CartItem(forms.ModelForm):
    class Meta:
        model = Model_Step01_CartItem
    fields = ["cart", "item_name", "item_quantity"]

Formset_CartItem = forms.inlineformset_factory(
    Model_Step01_Cart,
    Model_Step01_CartItem,
    form = Form_Step01_CartItem,
    extra = 3
    )

class Form_Step02(forms.ModelForm):
    class Meta:
        model = Model_Step02_Staffnote
    fields = ["note_staff"]

class Form_Step03(forms.ModelForm):
    class Meta:
        model = Model_Step03_Managernote
    fields = ["note_manager"]

views.py

class View_Step01(CreateView):
    # contains formset_factory for "Model_Step01_Cart" and "Model_Step01_CartItem"
    model = Model_Step01_Cart
    fields = ["cart_name"]

    # similar with the code displays here: https://medium.com/@adandan01/django-inline-formsets-example-mybook-420cc4b6225d

class View_Step02(CreateView):
    # gets info from step 01, and adds staff's note

class View_Step03(CreateView):
    # gets info from step 01 and 02, and adds manager's note.

1 个答案:

答案 0 :(得分:0)

Find my answer将单个表单拆分为多个视图。您可以配置gist script使其符合要求

对于基于类的视图映射,一个视图的成功URL到另一个CreateView.as_view(model=myModel, success_url="/<path-to-view2/")