Custom template in django form wizard - NameError

519 Views Asked by At

I am trying to create custom templates for a simple contact form as per the django docs but I am getting a NameError. Looks like a simple issue but I can't figure it out. Any help will be greatly appreciated. The error message is:

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

where 'wizardcustomtemplate' is the app. Here is my code:

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='[email protected]')

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

The form wizard works fine if I don't use the custom templates (FORMS, TEMPLATES etc.) Please let me know if you need additional information.

1

There are 1 best solutions below

0
On

Solved it by adding import wizardcustomtemplate in views.py as suggested by @Rohan.