405 for Google App Engine - 此资源不允许POST方法 - 添加到Boilerplate

时间:2013-06-04 15:49:53

标签: python google-app-engine web-applications webapp2 http-status-code-405

我对应用引擎不感兴趣,我正在努力为样板(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 %}

2 个答案:

答案 0 :(得分:0)

您是否检查过RedirectRoute以查看它是否在POST上进行任何可能会返回该错误的处理?

编辑: 我快速浏览了gae-boilerplate,它的main.py

中有额外的代码
routes.add_routes(app)
boilerplate_routes.add_routes(app)
admin_routes.add_routes(app)

最后两个功能可能会搞砸你的路线。如果您不知道它在做什么,我建议不要使用样板。

答案 1 :(得分:0)

我想我弄清楚了......当把这段代码粘贴到样板文件中时,我错误地切断了处理会话的处理程序 - 我相信这就是抛出错误的原因。