由于表单脚本而出现空白页

时间:2012-06-14 18:26:29

标签: python forms google-app-engine

app.yaml: 

     application: emot
        version: 1
        runtime: python
        api_version: 1

    handlers:
    -   url: /stylesheets/
        static_dir: stylesheets
    -   url: /.*
        script: index.py

index.yaml: 

    indexes:

    -   kind: Visitor 
        properties:
        -   name: name
        -   name: mood
        -   name: date
            direction: asc 

接下来,我的脚本:表单现在显示在页面上(好),但表单上的数据存储没有属性值提交。当我注释掉self.redirect时,获得POST 200状态(但仍然没有数据存储输入);当没有对此行发表评论时,POST 302状态。

#!/usr/bin/env python
import cgi
import time
import datetime 
import re
import urllib
import wsgiref.handlers
import cgitb; cgitb.enable()
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

#model
class Visitor(db.Model):
    name = db.StringProperty(required=1)
    mood = db.StringProperty(choices=["good","bad","fair"])
    date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write("""<html>><body>
<form action="/" method="post">
<p>First Name: <input type="text" name="name"/></p>
<p><input type="radio" name="mood" value="good">Good</p>
<p><input type="radio" name="mood" value="bad">Bad</p>
<p><input type="radio" name="mood" value="fair">Fair</p>
<p><input type="submit" value="Process"/></p>
</form>
</body></html> """)  
    def post(self):
        name = self.request.get("name")
        mood = self.request.get("mood")
        info = Visitor(name = name, mood = mood)
        info.put()
        self.redirect("/")                # POST 302 when no comment, 200 with comment

application = webapp.WSGIApplication([
    ("/", MainPage),
    ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
        main()

2 个答案:

答案 0 :(得分:1)

也许

  

self.response.out.write(

看起来你错过了这里的起点。如果在代码块之外定义了这个字符串文字,那么读起来会容易得多。

答案 1 :(得分:1)

真的不知道你要做什么,但是从函数调用中省略括号对你没有帮助。 这有效:

def get(self):
    self.response.out.write("""<html><body>
    <form action="/" method="post">
        <p>First Name: <input type="text" name="name"/></p>
        <p><input type="radio" name="mood" value="good">Good</p>
        <p><input type="radio" name="mood" value="bad">Bad</p>
        <p><input type="radio" name="mood" value="fair">Fair</p>
        <p><input type="submit"value="Process"></p>
    </form></body></html> """)