开发者

Django Form Preview with google app engine

I still didn't get django form preview with google app engine to work. I didn't see a working example and my attempts to use django form preview with google app engine failed since I couldn't translate the request handler usage from django to gae. I'm getting a form preview but it won't render:

        <html><body><form method="POST" action="/preview">
    <table>
<django.contrib.formtools.preview.FormPreview object at 0x3c5ca50>
    </table><input type="submit"></form></body></html>

The code that generates the HTML above is:

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 PreviewHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<html><body>'
                                '<form method="POST" '
                                'action="/preview">'
                                '<table>')
        self.response.out.write(FormPreview(ItemForm()))
        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&g开发者_运维技巧t;</html>' % id)

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

    run_wsgi_app(application)

How should I implement the PreviewHandler? Thank you in advance

UPDATE: Since django has form preview, could I take the template from the django framework and built on it or make my own template? For instance how ta enable that user presses the back button to make changes that also should initiate a validation

<input type="button" value="&lt;-- Go back" name="back" onClick="history.back()" />

<input type="submit" name="validate" value='{% trans "Go" %}' />


The Django documentation for FormPreview can be found here. It makes it clear that FormPreview is intended to be subclassed and used as a handler, which you can't do in webapp - it's specific to the Django framework. You certainly can't just instantiate a FormPreview object with your form and expect the string representation to be useful.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜