Django表单预览与谷歌应用引擎
我还是没能让Django的表单预览在Google App Engine上正常工作。我没有看到一个有效的例子,而我尝试将Django的表单预览应用到Google App Engine上也失败了,因为我无法把请求处理器的用法从Django转到GAE。我能看到一个表单预览,但它没有正确显示:
<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="<-- Go back" name="back" onClick="history.back()" />
<input type="submit" name="validate" value='{% trans "Go" %}' />
1 个回答
1
Django的文档中关于FormPreview的内容可以在这里找到。文档里说明了FormPreview是为了被继承和用作处理器的,这在普通的网页应用中是做不到的,因为它是专门为Django框架设计的。你不能仅仅创建一个FormPreview
对象,把你的表单放进去,然后期待它的字符串表示能有什么用。