我需要在此服务器中实现一些基本功能:
- 显示一个漂亮的主屏幕(html),带有 < select > 小部件和 2 个按钮(一个用于从高清中选择图像以上传,另一个用于发送)+ TextEditBox 用于输入消息
- 当用户单击“发送”时 - 从选择小部件中选择用户并
- 图片将被上传
我已经设法单独实现所有这些,现在当我让它们一起工作时,由于设计不好,我遇到了 app.yaml url 路径的问题。
app.yaml 看起来像:
runtime: python
handlers:
- url: /(.+)
static_files: images/\1
upload: images/(.*)
- url: /.*
script: kserver.py
kserver.py:
class StartPage(webapp.RequestHandler):
def get(self):
select_items = db.GqlQuery( "SELECT * FROM Registration" )
upload_url = blobstore.create_upload_url('/upload')
self.response.out.write(template.render("tst.html", {'select_items': select_items}))
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
image = self.request.get("img")
photo = Photo()
photo.imageblob = db.Blob(image)
photo.put()
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
class DownloadImage(webapp.RequestHandler):
def get(self):
photo= db.get(self.request.get("photo_id"))
if photo:
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(photo.imageblob)
else:
self.response.out.write("Image not available")
class Sender(webapp.RequestHandler):
def post(self):
...
...
self.response.headers['Content-Type'] = 'text/html' # reply with 200 OK
self.response.set_status( 200,"OK" )
...
...
class TokenService(webapp.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.set_status( 200,"Registration accepted" )
...
application = webapp.WSGIApplication([('/', StartPage),
('/sender',Sender),
('/upload', UploadHandler),
('/i', DownloadImage),
('/serve/([^/]+)?', ServeHandler),
('/token',TokenService)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
这是 tst.html :
<html>
<body>
<select name="accountName">
<option value=\"ALL\">ALL</option>
{% for item in select_items %}
<option value="{{ item.accountName }}">{{ item.accountName }}</option>
{% endfor %}
</select>
<form action="sender" method="POST">
<input type="text" id="TextEditBox_1">
<input type="submit" name="submit">
<input type="file" name="img" id="Choose_file">
</form>
</body>
</html>
请注意,标识很好,它只是代码的一部分。
在项目中,我有一个包含 app.ico 和背景图像(静态)的图像目录
好的,现在问题是我进入了 GAE 日志:
Static file referenced by handler not found: images/token
Static file referenced by handler not found: images/sender
这不是故意的。我认为它应该在'/'中
和
"/token 404 48ms 0kb No handlers matched this URL."
此外,当点击“发送”时,它会将我重定向到一个新页面:
The requested URL /sender was not found on this server.
此 /token 是来自外部的 POST,用于注册到服务器 - 服务器比将注册存储在 db 中。当按下“发送”按钮时,它应该使用这个 db.register 令牌。
欢迎对这个主题发表任何评论。谢谢!