GAE formpreview

时间:2011-01-15 09:53:25

标签: python django google-app-engine django-forms

我正在尝试使用Google App Engine启用表单预览。得到以下错误消息我怀疑在某处错误:

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

你能说出我的尝试有什么问题吗?这是一些代码。

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)),

更新:这是一个预览不起作用的完整应用。我们非常欢迎任何想法:

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)

新尝试生成了一些关于FormPreview对象的输出

<table><django.contrib.formtools.preview.FormPreview object at 0x5316fd0></table>

一旦编辑成预览处理程序

from django.contrib.formtools.preview import FormPreview
class PreviewHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<html><body>'
                                '<form method="POST" '
                                'action="/preview">'
                                '<table>')
        self.response.out.write(FormPreview(AForm))
        self.response.out.write('</table>'                                    
                                '<input type="submit">'
                                '</form></body></html>')

如果GAE支持django formtools和formpreview,那我该怎么办呢?谢谢你的回答。

1 个答案:

答案 0 :(得分:1)

您正在尝试将“AFormPreview”类的实例作为Web应用程序的处理程序类传递。你不能这样做 - 处理程序类需要是webapp.RequestHandler的子类。