0
由於main.app是默認頁面,無論根目錄中的index.html頁面(不幸的是GAE不像cgi/apache一樣工作),我已經制作了頁面main.app和查詢/響應頁面response.py。提交表格後,我得到錯誤:匹配app.yaml配置中的模式,POST 404錯誤
Not found error: /response.py did not match any patterns in application configuration.
application: emot13
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /stylesheets/
static_dir: stylesheets
- url:/
script: main.app
- url: /.
script: response.app
main.app:
#!/usr/bin/env python
import cgi
import urllib
from google.appengine.ext import db
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""<html>
<body>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body>
<form action="/response.py" method="post"> #also tried "response.py", no difference
<p>First Name: <input type="text" name="name"/></p>
<p>How are things?</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" name="submit" value="Process"/></p>
</form>
</body></html>""")
app = webapp2.WSGIApplication(
[("/", MainPage)],
debug=True)
def main():
application.run()
if __name__ == "__main__":
main()
response.py:
#!/usr/bin/env python
import cgi
import time
import datetime
import urllib
from google.appengine.ext import db
import webapp2
#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 Response(webapp2.RequestHandler):
def get(self):
today = datetime.date.today()
self.response.out.write("""<html><head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body>
self.response.out.write(today.strftime(<html><body><p style='color:#3E3535'>%A, %d %B</p>)
</body></html> """)
localtime = time.localtime(time.time())
mon = localtime[1] # MONTH
h = localtime[3] # HOUR
name = self.request.get("name")
name = name.capitalize()
mood = self.request.get("mood")
# variables and if/elif statements follow; they all work so that is not the problem.
responses = db.GqlQuery("SELECT * "
"FROM Visitor "
"ORDER BY date DESC_LIMIT 1")
for response in responses:
if mood == "bad" and name != "":
# responses follow; they all work so that is not the problem.
class Process(webapp2.RequestHandler):
def post(self):
name = self.request.get("name")
mood = self.request.get("mood")
info = Visitor(name = name, mood = mood)
info.put()
self.redirect("/")
app = webapp2.WSGIApplication(
[("/", Response),
("/", Process)],
debug=True)
# tried uncommenting this as well v v
#def response():
# application.run()
#if __name__ == "__response__":
# response()
幫助,將不勝感激。
當嘗試使用url /.*或/response.py時,在提交表單後,我會在response.py上收到「404 Not Found 資源找不到」。什麼可能導致這個?我的所有工作代碼都張貼在上面。表單在app.yaml中使用1個頁面效果很好,但我需要處理查詢並在response.py上顯示。 – p1nesap
另一個問題是您在Response中定義了一個獲取處理程序,但是您使用method = POST提交了您的表單,因此您需要在Response類中使用post()方法。 –