gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:27:49 25 4
gpt4 key购买 nike

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

我得到的唯一回应是:

[23/Jan/2016 11:06:50]"GET /registration/wizard HTTP/1.1" 200 13729

和一个空白页。浏览器或 Eclipse 控制台中没有错误。

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

提前致谢

我做了什么?

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

django-formtools==1.0
Django==1.8.3

按照官方文档的说明:

  1. 定义表单类

    registration/forms.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)
  2. 创建向导 View

    registration/views.py

    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
  3. 创建模板

    注册/test_step1.html

    <h1>Two fields form</h1>
    <input id="first_field" name="first_field">
    <input id="second_field" name="second_field">

    注册/test_step2.html

    <h1>Message form</h1>
    <input id="message" name="message">

    注册/test_wizard.html

    {% 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 %}
  4. 将“formtools”添加到我的 INSTALLED_APPS

    settings.py

    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
  5. 将我的 URLconf 指向您的 WizardView as_view() 方法。

注册/urls.py

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

对于第二种方式...

registration/views.py

def wizard(request):
return WizardTest.as_view(TEST_FORMS)(request)

最佳答案

您的模板遵循什么示例? The basic layout is in the docs .尝试从这样的开始(如果您对 formtools 生成的字段没问题,两个页面可以使用相同的模板):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Wizard</title>
{{ wizard.form.media }}

</head>
<body>
{% load i18n %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" name="submit" value="{% trans "submit" %}"/>
</form>
</body>
</html>

关于python - Django formtools 向导生成空白页(未返回错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34962499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com