这个问题与之前在这里提出并回答的问题高度相关:如何将 FormWizard 包装在视图中?
有人可以发布他们如何成功地将 Django 表单向导包装到视图中的确切详细信息,以便可以使用 login_required 装饰器吗?互联网上有很多关于这个主题的讨论,但它们似乎都不完整,因为它们实际上并没有展示他们是如何定义表单向导类的。
当我将浏览器指向视图时,出现以下异常:
__init__() takes exactly 1 non-keyword argument (2 given) in views.py line #108
当我实例化我的表单向导对象时我传递了哪些参数,这样它就不会给我这个错误?如果您有一些有效的示例代码,请发布。
这是我的 urls.py 文件中的内容:
url(r'^createObject/$', views.createObjectView, name='createObject'),
这是我的 views.py 文件中的内容:
CREATE_OBJECT_FORMS = [
("createMyForm0", createObjectForm0),
("createMyForm1", createObjectForm1),
("createMyForm2", createObjectForm2),
("createMyForm3", createObjectForm3),
]
CREATE_OBJECT_TEMPLATES = {
"createMyForm0": "myApp/form0.html",
"createMyForm1": "myApp/form1.html",
"createMyForm2": "myApp/form2.html",
"createMyForm3": "myApp/form3.html",
}
@login_required
def createObjectView(request):
# Set up the dictionary of initial data for the form
# In this case, we are pre-filling some data from the first form only
initial = {0: {}}
# Create the form wizard
form = createObjectWizard(
[
createObjectForm0,
createObjectForm1,
createObjectForm2,
createObjectForm3,
],
initial=initial # This is Line #108
)
# Call the form wizard passing through the context and the request
return form(context=RequestContext(request), request=request)
class createObjectWizard(SessionWizardView):
def get_template_names(self):
return [CREATE_OBJECT_TEMPLATES[self.steps.current]]
def done(self, form_list, **kwargs):
doSomethingFunction(form_list)
return HttpResponseRedirect('/objectCreated/')