我对应用程序引擎很陌生,并且正在努力向样板文件(https://github.com/coto/gae-boilerplate)添加一个简单的聊天功能。聊天应用程序本身可以正常工作,但是当我尝试将其添加到样板文件时,当我尝试向 ChatHandler 发布聊天消息时收到 405 POST 方法错误。chatscreen.html 最初从 ChatHander 渲染没有问题,就在我尝试发布聊天时,我得到了错误。感谢您提供的任何建议-
这是python聊天代码:
import httpagentparser
from boilerplate import models
from boilerplate.lib.basehandler import BaseHandler
from boilerplate.lib.basehandler import user_required
import cgi
import urllib
import os
import logging
import webapp2
import jinja2
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.api import memcache
class ChatMessage(db.Model):
user = db.StringProperty()
text = db.StringProperty()
created = db.DateTimeProperty(auto_now=True)
class User(db.Model):
account = db.StringProperty()
user = db.StringProperty()
class ChatHandler(BaseHandler):
def get(self, **kwargs):
params = {
}
return self.render_template('chatscreen.html', **params)
def post(self, **kwargs):
user = self.request.get('user')
self.session['user'] = user
if not 'user' in self.session:
params = {
'error' : 'Must be logged in'
}
return self.render_template('chatscreen.html', **params)
return
msg = self.request.get('message')
if msg == '':
params = {
'error' : 'Blank message ignored'
}
return self.render_template('chatscreen.html', **params)
return
newchat = ChatMessage(user = "temp_user3", text=msg)
newchat.put();
if newchat.put():
chat_list = ChatMessage.all().order('-created').fetch(100) #pulling chat messages to include new ones not yet in memcache
memcache.set('chat_list_key', chat_list, time=2000000)
logging.info('entry added by user -- memcache & datastore updated with new list')
params = {
}
return self.render_template('chatscreen.html', **params)
class MessagesHandler(BaseHandler):
def get(self, **kwargs):
chat_list = memcache.get('chat_list_key') #checking cache for chat_list
# if no chat_list then pull a list from datastore and populate memcache
if chat_list is None:
chat_list = ChatMessage.all().order('-created').fetch(100)
memcache.set('chat_list_key', chat_list, time=2000000)
logging.info('Saving chat_list to memcache *** no messages found')
params = {
'chat_list': chat_list
}
return self.render_template('messagelist.html', **params)
#if chat_list exist then pull data from memcache
else:
params = {
'chat_list': chat_list
}
return self.render_template('messagelist.html', **params)
logging.info('Got chat_list from cache')
class MainHandler(BaseHandler):
def get(self):
user = self.session.get('user')
params = {
'user': user
}
return self.render_template('index.html', **params)
这是路由:
from webapp2_extras.routes import RedirectRoute
from web import handlers
secure_scheme = 'https'
_routes = [
RedirectRoute('/chat/', handlers.ChatHandler, name='chat', strict_slash=True),
RedirectRoute('/messages/', handlers.MessagesHandler, name='messages', strict_slash=True),
RedirectRoute('/secure/', handlers.SecureRequestHandler, name='secure', strict_slash=True)
]
def get_routes():
return _routes
def add_routes(app):
if app.debug:
secure_scheme = 'http'
for r in _routes:
app.router.add(r)
这是chatscreen.html:
{% extends "_base.html" %}
{% block bodycontent %}
<h1>AppEngine Chat</h1>
<form method="post" action="/chat">
<p>
<input type="text" name="message" size="60"/>
<input type="submit" value="Chat"/>
</p>
</form>
{% if not error == None %}
<p>
{{ error }}
</p>
{% endif %}
<div id="chatcontent">
Loading...
<!-- <img src="/static/spinning.gif" alt="Loading..."/> -->
</div>
<script> /* <![CDATA[ */
$(document).ready(function(){
$(function() {
updateMsg();
});
function updateMsg() {
$('#chatcontent').load('/messages');
setTimeout(updateMsg, 5000);
}
});//end doc.onready function
/* ]]> */
</script>
{% endblock %}
这是messagelist.html:
{% for chat in chat_list %}
<p>
<strong>{{ chat.user}}</strong>:
<br /> {{ chat.text }}
</p>
{% endfor %}