Django Form Preview
Posted
by Mark Kecko
on Stack Overflow
See other posts from Stack Overflow
or by Mark Kecko
Published on 2010-06-10T19:33:11Z
Indexed on
2010/06/11
14:22 UTC
Read the original article
Hit count: 425
django
|django-forms
I'm trying to use django's FormPreview and I can't get it to work properly. Here's my code:
forms.py
class MyForm(forms.ModelForm):
status = forms.TypedChoiceField(
coerce=int, choices=LIST_STATUS, label="type",
widget=forms.RadioSelect
)
description = forms.CharField(widget = forms.Textarea)
stage = forms.CharField()
def __init__(self, useradd=None, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['firm'].label = "Firm"
class Meta:
model = MyModel
fields = ['status', 'description', 'stage']
class MyFormPreview(FormPreview):
form_template = 'templates/post.html'
preview_template = 'templates/review.html'
def process_preview(self, request, cleaned_data):
print "processed"
def done(self, request, cleaned_data):
print "done"
# Do something with the cleaned_data, then redirect
# to a "success" page.
return HttpResponseRedirect('/')
urls.py
(r'^post/$', MyFormPreview(MyForm)),
post.html
<form id = "post_ad" action = "" method = "POST" enctype="multipart/form-data">
<table>
{{form.as_table}}
</table>
<input type="submit" name="save" value="Post" />
</form>
When I go to /post/ I get the correct form and I fill it out. When I submit the form it goes right back to /post/ but but there are no errors (I've tried displaying {{errors}}) and the form is empty. None of my print statements execute. I'm not sure what I'm missing. Can anyone help me out? I can't find any documentation besides what's on the django site.
Also, what's the "preview" variable called that I should use in my preview.html template? {{preview}} or do I just do {{form}} again? -- Answered below.
I tried adding 'django.contrib.formtools' to my installed_apps in settings and I tried using the code from the default form templates from django.contrib as suggested below. Still, when I submit the form I go right back to the post template, none of my print statements execute :(
© Stack Overflow or respective owner