GAE formpreview

Posted by Niklas R on Stack Overflow See other posts from Stack Overflow or by Niklas R
Published on 2011-01-15T09:53:25Z Indexed on 2011/01/17 1:53 UTC
Read the original article Hit count: 575

I'm trying to enable form preview with Google App Engine. Getting the following error message I suspect being mistaken somewhere:

   ... handler = handler_class()
TypeError: __call__() takes at least 2 arguments (1 given)

Can you tell what's wrong with my attempt? Here is some of the code.

from django.contrib.formtools.preview import FormPreview
class AFormPreview(FormPreview): 
  def done(self, request, cleaned_data):
      # Do something with the cleaned_data, then redirect
      # to a "success" page.
      self.response.out.write('Done!')

class AForm(djangoforms.ModelForm):  
  text = forms.CharField(widget=forms.Textarea(attrs={'rows':'11','cols':'70','class':'foo'}),label=_("content").capitalize())

  def clean(self):
      cleaned_data = self.clean_data
      name = cleaned_data.get("name")
      if not name:
          raise forms.ValidationError("No name.")

      # Always return the full collection of cleaned data.
      return cleaned_data

  class Meta:
        model = A
        fields = ['category','currency','price','title','phonenumber','postaladress','name','text','email'] #change the order


...

('/aformpreview/([^/]*)', AFormPreview(AForm)),

UPDATE: Here's a complete app where the preview is not working. Any ideas are most welcome:

import cgi

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext.db import djangoforms

class Item(db.Model):
    name = db.StringProperty()
    quantity = db.IntegerProperty(default=1)
    target_price = db.FloatProperty()
    priority = db.StringProperty(default='Medium',choices=[
      'High', 'Medium', 'Low'])
    entry_time = db.DateTimeProperty(auto_now_add=True)
    added_by = db.UserProperty()

class ItemForm(djangoforms.ModelForm):
    class Meta:
        model = Item
        exclude = ['added_by']

from django.contrib.formtools.preview import FormPreview
class ItemFormPreview(FormPreview): 
    def done(self, request, cleaned_data):
        # Do something with the cleaned_data, then redirect
        # to a "success" page.
        return HttpResponseRedirect('/')

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<html><body>'
                                '<form method="POST" '
                                'action="/">'
                                '<table>')
        # This generates our shopping list form and writes it in the response
        self.response.out.write(ItemForm())
        self.response.out.write('</table>'
                                '<input type="submit">'
                                '</form></body></html>')
    def post(self):
        data = ItemForm(data=self.request.POST)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.added_by = users.get_current_user()
            entity.put()
            self.redirect('/items.html')
        else:
            # Reprint the form
            self.response.out.write('<html><body>'
                                    '<form method="POST" '
                                    'action="/">'
                                    '<table>')
            self.response.out.write(data)
            self.response.out.write('</table>'
                                    '<input type="submit">'
                                    '</form></body></html>')

class ItemPage(webapp.RequestHandler):
    def get(self):
        query = db.GqlQuery("SELECT * FROM Item ORDER BY name")
        for item in query:
            self.response.out.write('<a href="/edit?id=%d">Edit</a> - ' %
                                    item.key().id())
            self.response.out.write("%s - Need to buy %d, cost $%0.2f each<br>" %
                                    (item.name, item.quantity, item.target_price))

class EditPage(webapp.RequestHandler):
    def get(self):
        id = int(self.request.get('id'))
        item = Item.get(db.Key.from_path('Item', id))
        self.response.out.write('<html><body>'
                                '<form method="POST" '
                                'action="/edit">'
                                '<table>')
        self.response.out.write(ItemForm(instance=item))
        self.response.out.write('</table>'
                                '<input type="hidden" name="_id" value="%s">'
                                '<input type="submit">'
                                '</form></body></html>' % id)

    def post(self):
      id = int(self.request.get('_id'))
      item = Item.get(db.Key.from_path('Item', id))
      data = ItemForm(data=self.request.POST, instance=item)
      if data.is_valid():
          # Save the data, and redirect to the view page
          entity = data.save(commit=False)
          entity.added_by = users.get_current_user()
          entity.put()
          self.redirect('/items.html')
      else:
          # Reprint the form
          self.response.out.write('<html><body>'
                                  '<form method="POST" '
                                  'action="/edit">'
                                  '<table>')
          self.response.out.write(data)
          self.response.out.write('</table>'
                                  '<input type="hidden" name="_id" value="%s">'
                                  '<input type="submit">'
                                  '</form></body></html>' % id)

def main():
    application = webapp.WSGIApplication(
                                         [('/', MainPage),
                                          ('/edit', EditPage),
                                          ('/items.html', ItemPage),
                                          ('/itemformpreview', ItemFormPreview(ItemForm)),
                                          ],
                                         debug=True)

    run_wsgi_app(application)

© Stack Overflow or respective owner

Related posts about python

Related posts about django