How wrap a FormWizard in a View?

流过昼夜 提交于 2019-12-07 16:27:08

问题


How do I wrap a Django Form Wizard in a view? I need to do this so I can access request.

Does anyone have some example code for this?


回答1:


I probably should be just commenting on Manoj's answer, but sounds you need code

urls.py

from django.conf.urls.defaults import *
from MyApp import views

urlpatterns = patterns(
  '',
  (r'^wizard/$', views.MyWizardView ),
)

views.py

@login_required    
def MyWizardView (request):
  cw = MyWizard([WizardName, WizardQuestions, WizardProvider, WizardGoomber])
  return cw(request)



回答2:


The as_view function converts a class based view into a callable view:

from django import forms
from django.contrib.formtools.wizard.views import SessionWizardView

class Form1(forms.Form):
    a = forms.CharField()

class Form2(forms.Form):
    b = forms.CharField()

class MyWizard(SessionWizardView):
    pass

wizard_view = MyWizard.as_view([Form1, Form2])

def view(request):
    # do something fancy with the request object here
    return wizard_view(request)

This is basicly the same answer as in How to wrap a Django Form Wizard in a View?




回答3:


This Django snippet may prove useful.

From the title: "FormWizard inside view with proper context handling and site templating support, without having to use urls.py"



来源:https://stackoverflow.com/questions/3503964/how-wrap-a-formwizard-in-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!