开发者

Django Form Preview

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 = MyMod开发者_开发知识库el
    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 :(


See django/contrib/formtools/templates for the default templates.


Formtools has been removed from 1.8+ of Django.

Now accessible at the Github page: Django-Formtools and its related documentation can be found at read the docs.

sudo pip install django-formtools should help you through getting it installed.

The removal was announced in the 1.8 release highlights

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜