Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django formtools向导生成空白页(未返回错误)_Python_Django_Django Forms_Django Formwizard - Fatal编程技术网

Python Django formtools向导生成空白页(未返回错误)

Python Django formtools向导生成空白页(未返回错误),python,django,django-forms,django-formwizard,Python,Django,Django Forms,Django Formwizard,我试图在formtools包中使用formwizard,但没有成功(在早期版本中,当包在Django中时,我就能够这样做) 我得到的唯一答复是: [23/Jan/2016 11:06:50]"GET /registration/wizard HTTP/1.1" 200 13729 还有一张空白页。浏览器或Eclipse控制台中没有错误 谷歌搜索不可能没有错误。请帮忙 提前谢谢 我做了什么? 首先,我用pip安装了formtools包: django-formtools==1.0 Django=

我试图在formtools包中使用formwizard,但没有成功(在早期版本中,当包在Django中时,我就能够这样做)

我得到的唯一答复是:

[23/Jan/2016 11:06:50]"GET /registration/wizard HTTP/1.1" 200 13729
还有一张空白页。浏览器或Eclipse控制台中没有错误

谷歌搜索不可能没有错误。请帮忙

提前谢谢

我做了什么?

首先,我用pip安装了formtools包:

django-formtools==1.0
Django==1.8.3
按照官方文件的指示:

  • 定义表单类

    注册/表格.py

    class StepForm1(forms.Form):
        first_field = forms.CharField(max_length=100)
        second_field = forms.CharField()
    
    class StepForm2(forms.Form):
        message = forms.CharField(widget=forms.Textarea)
    
    TEST_TEMPLATES = {"test_step_1": "registration/test_step1.html", "test_step_2": "registration/test_step2.html", }
    
    from formtools.wizard.views import SessionWizardView
    
    class WizardTest(SessionWizardView):
        template_name = 'registration/test_wizard.html'
    
        # Return templates for each step
        def get_templates_name(self):
            return [TEST_TEMPLATES[self.steps.current]]
    
        # Method called when all is done
        def done(self, form_list, **kwargs):
    
            # return HttpResponseRedirect('/url-to-redirect-to/') 
    
            # We return the final template with the info
            return render_to_response('test_done.html', {
                                       'form_data':[form.cleaned_data for form in form_list],
                                       })
    
        # THESE METHODS BELOW ARE NOT NEEDED, BUT COMMENTED FOR FUTURE USE
    
        # Not strictly needed. Returns data for a step
        # or None if form is not valid
        # def get_cleaned_data_for_step(self, step):
        #     return None
    
        # Form data postprocessing in a concrete wizard step
        # def process_step(self, form):
        #     return self.get_form_step_data(form)
    
        # Handles value from a step before storing them into wizard
        # def get_form_step_data(self, form):
        #     return form.data
    
    DJANGO_APPS = (
        # Default Django apps:
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'formtools',                 # <===== HERE
    
        # Useful template tags:
        # 'django.contrib.humanize',
    
        # Admin panel and documentation:
        'django.contrib.admin',
        # 'django.contrib.admindocs',
    )
    
    # Apps specific for this project go here.
    LOCAL_APPS = (
        'person',
        'registration',
        'teaching',
        'utils',
    )
    
    # See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
    INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
    
    from registration.forms import StepForm1, StepForm2
    TEST_FORMS = [("test_step_1", StepForm1), ("test_step_2", StepForm2), ]
    
    from registration.views import WizardTest
    
    # I tried in two ways, none of them worked
    
    urlpatterns = patterns('',
             url(r'^wizard$', WizardTest.as_view(TEST_FORMS), name='wizard_test'),
             url(r'^wizard2$', views.wizard, name='wizard_test'),
    )
    
    def wizard(request):
        return WizardTest.as_view(TEST_FORMS)(request)
    
  • 创建向导视图

    注册/视图.py

    class StepForm1(forms.Form):
        first_field = forms.CharField(max_length=100)
        second_field = forms.CharField()
    
    class StepForm2(forms.Form):
        message = forms.CharField(widget=forms.Textarea)
    
    TEST_TEMPLATES = {"test_step_1": "registration/test_step1.html", "test_step_2": "registration/test_step2.html", }
    
    from formtools.wizard.views import SessionWizardView
    
    class WizardTest(SessionWizardView):
        template_name = 'registration/test_wizard.html'
    
        # Return templates for each step
        def get_templates_name(self):
            return [TEST_TEMPLATES[self.steps.current]]
    
        # Method called when all is done
        def done(self, form_list, **kwargs):
    
            # return HttpResponseRedirect('/url-to-redirect-to/') 
    
            # We return the final template with the info
            return render_to_response('test_done.html', {
                                       'form_data':[form.cleaned_data for form in form_list],
                                       })
    
        # THESE METHODS BELOW ARE NOT NEEDED, BUT COMMENTED FOR FUTURE USE
    
        # Not strictly needed. Returns data for a step
        # or None if form is not valid
        # def get_cleaned_data_for_step(self, step):
        #     return None
    
        # Form data postprocessing in a concrete wizard step
        # def process_step(self, form):
        #     return self.get_form_step_data(form)
    
        # Handles value from a step before storing them into wizard
        # def get_form_step_data(self, form):
        #     return form.data
    
    DJANGO_APPS = (
        # Default Django apps:
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'formtools',                 # <===== HERE
    
        # Useful template tags:
        # 'django.contrib.humanize',
    
        # Admin panel and documentation:
        'django.contrib.admin',
        # 'django.contrib.admindocs',
    )
    
    # Apps specific for this project go here.
    LOCAL_APPS = (
        'person',
        'registration',
        'teaching',
        'utils',
    )
    
    # See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
    INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
    
    from registration.forms import StepForm1, StepForm2
    TEST_FORMS = [("test_step_1", StepForm1), ("test_step_2", StepForm2), ]
    
    from registration.views import WizardTest
    
    # I tried in two ways, none of them worked
    
    urlpatterns = patterns('',
             url(r'^wizard$', WizardTest.as_view(TEST_FORMS), name='wizard_test'),
             url(r'^wizard2$', views.wizard, name='wizard_test'),
    )
    
    def wizard(request):
        return WizardTest.as_view(TEST_FORMS)(request)
    
  • 创建模板

    注册/测试步骤1.html

    <h1>Two fields form</h1>
    <input id="first_field" name="first_field">
    <input id="second_field" name="second_field">
    
    <h1>Message form</h1>
    <input id="message" name="message">
    
    {% extends "person/alumnos.html" %}
    {% load i18n %}
    
    {% block head %}
        {{ wizard.form.media }}
    {% endblock head %}
    
    {% block content %}
    <p>{% trans "Step {{wizard.steps.step1}} of {{wizard.steps.count}}" %}</p>
    
    <form action="" method="post">
    {% csrf_token %}
    
    {{ wizard.management_form }}
    
    {% if wizard.form.forms  %}
        {{ wizard.form.management_form }}
        {% for form in wizard.form.forms %}
            {{form}}
        {% endfor %}
    {% else %}
        {{ wizard.form }}
    {% endif %}
    
    {% if wizard.steps.prev %}
    <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "Beginning" %}</button>
    <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "Previous step" %}</button>
    {% endif %}
    <input type="submit" value="submit"/>
    
    </form>
    
    {% endblock %}
    
    第二条路

    注册/视图.py

    class StepForm1(forms.Form):
        first_field = forms.CharField(max_length=100)
        second_field = forms.CharField()
    
    class StepForm2(forms.Form):
        message = forms.CharField(widget=forms.Textarea)
    
    TEST_TEMPLATES = {"test_step_1": "registration/test_step1.html", "test_step_2": "registration/test_step2.html", }
    
    from formtools.wizard.views import SessionWizardView
    
    class WizardTest(SessionWizardView):
        template_name = 'registration/test_wizard.html'
    
        # Return templates for each step
        def get_templates_name(self):
            return [TEST_TEMPLATES[self.steps.current]]
    
        # Method called when all is done
        def done(self, form_list, **kwargs):
    
            # return HttpResponseRedirect('/url-to-redirect-to/') 
    
            # We return the final template with the info
            return render_to_response('test_done.html', {
                                       'form_data':[form.cleaned_data for form in form_list],
                                       })
    
        # THESE METHODS BELOW ARE NOT NEEDED, BUT COMMENTED FOR FUTURE USE
    
        # Not strictly needed. Returns data for a step
        # or None if form is not valid
        # def get_cleaned_data_for_step(self, step):
        #     return None
    
        # Form data postprocessing in a concrete wizard step
        # def process_step(self, form):
        #     return self.get_form_step_data(form)
    
        # Handles value from a step before storing them into wizard
        # def get_form_step_data(self, form):
        #     return form.data
    
    DJANGO_APPS = (
        # Default Django apps:
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'formtools',                 # <===== HERE
    
        # Useful template tags:
        # 'django.contrib.humanize',
    
        # Admin panel and documentation:
        'django.contrib.admin',
        # 'django.contrib.admindocs',
    )
    
    # Apps specific for this project go here.
    LOCAL_APPS = (
        'person',
        'registration',
        'teaching',
        'utils',
    )
    
    # See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
    INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
    
    from registration.forms import StepForm1, StepForm2
    TEST_FORMS = [("test_step_1", StepForm1), ("test_step_2", StepForm2), ]
    
    from registration.views import WizardTest
    
    # I tried in two ways, none of them worked
    
    urlpatterns = patterns('',
             url(r'^wizard$', WizardTest.as_view(TEST_FORMS), name='wizard_test'),
             url(r'^wizard2$', views.wizard, name='wizard_test'),
    )
    
    def wizard(request):
        return WizardTest.as_view(TEST_FORMS)(request)
    

    对于模板,您遵循的示例是什么。尝试从以下内容开始(如果您对formtools生成的字段没有问题,两个页面可以使用相同的模板):

    
    表单向导
    {{wizard.form.media}
    {%load i18n%}
    {{wizard.steps.count}的步骤{wizard.steps.step1}

    {%csrf_令牌%} {{wizard.management_form} {%if wizard.form.forms%} {{wizard.form.management_form} {wizard.form.forms%中窗体的%s} {{form}} {%endfor%} {%else%} {{wizard.form} {%endif%} {%if wizard.steps.prev%} {%trans“第一步”%} {%trans“上一步”%} {%endif%}
    我发现了我的错误。我输入了获取模板的方法名。不是

    get_templates_name(self)
    
    但是

    它并没有得到任何模板,但当返回到服务器时,它回答了HTTP200OK,因为它并没有发现任何错误


    我还发现,我的每个步骤的模板都应该扩展主向导模板,以便正常工作。没有必要使用“完成”模板。

    导致
    self.cleaned_data['field']
    为空的主要问题是,我需要使用
    form.field.html_name
    而不是
    form.field.name
    作为模板的名称字段

    然而,从Django formtools提供的文档来看,这并不是很清楚。以下是我的模板页面的摘录,因此更清晰:

    <div class="form-group">
      <label>{{ form.account_name.label }}</label>
      <input name="{{ form.account_name.html_name }}"
             value="{{ form.account_name.value|default_if_none:'' }}"
             class="form-control">
    </div>
    
    
    {{form.account_name.label}
    
    我编辑了帖子以显示向导模板。是的,我上次的评论被编辑了,因为它可能被误解了。我处于相同的情况,表单的主模板一直都是相同的(我只是在创建表单时忘记将其放在stackoverflow帖子中)