如何在视图中包装 Django 表单向导? [英] How to wrap a Django Form Wizard in a View?

查看:22
本文介绍了如何在视图中包装 Django 表单向导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与之前在这里提出并回答的问题高度相关:如何在视图中包装 FormWizard?

This question is highly related to one that was previously asked and answered here: How wrap a FormWizard in a View?

有人可以发布他们如何成功地将 Django 表单向导包装到视图中以便可以使用 login_required 装饰器的确切细节吗?互联网上有很多关于这个话题的讨论,但它们似乎都不完整,因为它们实际上并没有展示他们是如何定义表单向导类的.

Can someone post exact details of how they have successfully wrapped a Django Form Wizard into a view so that the login_required decorator can be used? There are numerous discussions of this topic on the internet but they all seem to be incomplete because they don't actually show how they have defined their Form Wizard class.

当我将浏览器指向视图时,出现以下异常:

When I run point my browser to the view, I get the following exception:

__init__() takes exactly 1 non-keyword argument (2 given) in views.py line #108

当我实例化我的 Form Wizard 对象时,我传递了哪些参数,以便它不会给我这个错误?如果您有一些有效的示例代码,请发布.

What arguments do I pass when I instantiate my Form Wizard object such that it doesn't give me this error? If you have some sample code that works, please post it.

这是我的 urls.py 文件中的内容:

Here is whats in my urls.py file:

url(r'^createObject/$', views.createObjectView, name='createObject'),

这是我的 views.py 文件中的内容:

Here is whats in my views.py file:

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/')

推荐答案

as_view 函数将基于类的视图转换为可调用视图:

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

from django import forms
from django.contrib.auth.decorators import login_required
from django.contrib.formtools.wizard.views import SessionWizardView
from django.template.response import TemplateResponse

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

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

FORMS = [("step1", Form1),
         ("step2", Form2)]

TEMPLATES = {"step1": "wizard_step.html",
             "step2": "wizard_step.html"}

class MyWizard(SessionWizardView):

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list):
        # get data from forms
        a = self.get_cleaned_data_for_step('step1')['a']
        b = self.get_cleaned_data_for_step('step2')['b']
        # access the request as self.request
        request = self.request
        # (...)
        # return response
        return TemplateResponse(request, 'wizard_success.html', {
            'a': a,
            'b': a
        })

wizard_view = MyWizard.as_view(FORMS)

@require_login
def wrapped_wizard_view(request):
    return wizard_view(request)

myapp/templates/wizard_step.html

{% extends "base.html" %}
{% load i18n %}

{% block content %}

<form method="post">
{% include "formtools/wizard/wizard_form.html" %}
</form>

{% endblock %}

myapp/urls.py

from django.conf.urls import patterns, url

urlpatterns = patterns('myapp.views',
    url(r'^wizard/$', 'wrapped_wizard_view'),
)

这篇关于如何在视图中包装 Django 表单向导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆