django表单向导中的自定义模板 - NameError

时间:2014-01-17 06:48:05

标签: django forms django-templates django-formwizard formwizard

我正在尝试根据django docs为简单的联系表单创建自定义模板,但我得到的是NameError。看起来像一个简单的问题,但我无法弄清楚。任何帮助将不胜感激。错误消息是:

"NameError at /contact/
name 'wizardcustomtemplate' is not defined"

其中'wizardcustomtemplate'是应用。这是我的代码:

urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

from wizardcustomtemplate.forms import SubjectForm, SenderForm, MessageForm
from wizardcustomtemplate.views import ContactWizard

urlpatterns = patterns('',
                       url(r'^admin/', include(admin.site.urls)),
                       url(r'^contact/$', ContactWizard.as_view(FORMS)),
)

views.py

import os
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.core.mail import send_mail
from django.core.context_processors import csrf
from django.contrib.formtools.wizard.views import SessionWizardView
from django.contrib.formtools.wizard.views import WizardView
from django.core.files.storage import FileSystemStorage
from django.core.files import File

FORMS = [("0", wizardcustomtemplate.forms.SubjectForm),
("1", wizardcustomtemplate.forms.SenderForm),
("2", wizardcustomtemplate.forms.MessageForm)
]

TEMPLATES = {"0": "wizardcustomtemplate/subject.html",
"1": "wizardcustomtemplate/sender.html",
"2": "wizardcustomtemplate/message.html"
}

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

    def done(self, form_list, **kwargs):
        form_data = process_form_data(form_list)
        return render_to_response('wizardcustomtemplate/thanks.html', {'form_data': form_data})

def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]
    return form_data

forms.py

from django import forms

class SubjectForm(forms.Form):
    subject = forms.CharField(max_length = 100,initial='Wizard')

class SenderForm(forms.Form):
    sender = forms.EmailField(initial='abcd@efgh.org')

class MessageForm(forms.Form):
    message = forms.CharField(initial='How r u?')

如果我不使用自定义模板(FORMS,TEMPLATES等),表单向导工作正常。如果您需要其他信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

按照@Rohan的建议,在views.py中添加import wizardcustomtemplate解决了这个问题。