簡體   English   中英

帶有Google App Engine的Django Form Preview

[英]Django Form Preview with google app engine

我仍然沒有使用Google App Engine進行Django表單預覽的工作。 我沒有看到有效的示例,並且由於無法將django的請求處理程序用法轉換為gae,因此我嘗試將django表單預覽與google app引擎結合使用的嘗試失敗。 我正在獲取表單預覽,但無法渲染:

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

上面生成HTML的代碼是:

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></html>' % id)

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

    run_wsgi_app(application)

我應該如何實現PreviewHandler? 先感謝您

更新:由於django具有表單預覽功能,我可以從django框架中獲取模板並在其之上構建還是制作自己的模板? 例如,如何使用戶按下“后退”按鈕進行更改,這些更改也應啟動驗證

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

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

可以在這里找到FormPreview的Django文檔。 很清楚,FormPreview旨在被子類化並用作處理程序,而這在webapp中是無法做到的-它特定於Django框架。 您當然不能只使用表單實例化FormPreview對象並期望字符串表示形式有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM