django 表单向导中的自定义模板 - NameError
Posted
技术标签:
【中文标题】django 表单向导中的自定义模板 - NameError【英文标题】:Custom template in django form wizard - NameError 【发布时间】:2014-02-06 09:49:39 【问题描述】:我正在尝试根据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 等),表单向导可以正常工作。如果您需要更多信息,请告诉我。
【问题讨论】:
我认为您需要在 views.py 中添加import wizardcustomtemplate
谢谢罗汉。成功了!!
【参考方案1】:
按照@Rohan 的建议在views.py 中添加import wizardcustomtemplate
解决了这个问题。
【讨论】:
以上是关于django 表单向导中的自定义模板 - NameError的主要内容,如果未能解决你的问题,请参考以下文章