Home > OS >  How to properly implement Django form session wizard
How to properly implement Django form session wizard

Time:07-18

I've been attempting to construct a multi-step form using the Django session wizard for hours, but I keep getting the error, AttributeError: 'function' object has no property 'as_view'. I'm not sure why this mistake occurred. Any ideas?

enter image description here

enter image description here

enter image description here

views

from django.shortcuts import render
from formtools.wizard.views import SessionWizardView
from .forms import WithdrawForm1, WithdrawForm2


class WithdrawWizard(SessionWizardView):
    template_name = 'withdraw.html'
    form_list = [WithdrawForm1, WithdrawForm2]

    def done(self, form_list, **kwargs):
        form_data = [form.cleaned_data for form in form_list]
        
        return render(self.request, 'done.html', {'data': form_data})

forms

from django import forms
from .models import Investment, Withdraw
from .models import WithdrawStepOne, WithdrawStepTwo


class WithdrawForm1(forms.ModelForm):
    class Meta:
        model = WithdrawStepOne
        fields = ['investment_id',]


class WithdrawForm2(forms.ModelForm):
    class Meta:
        model = WithdrawStepTwo
        fields = [
            'proof_of_address',
            'user_pic'
        ]

urls

from django.urls import path
from .forms import WithdrawForm1, WithdrawForm2
from . import views


urlpatterns = [
    path('withdraw/', views.WithdrawWizard.as_view(), name='withdraw'),
]

CodePudding user response:

You used @login_required decorator on WithdrawWizard class, but the decorator works only for function based views.

Use LoginRequiredMixin for class based views.

  • Related