2012-09-18 3 views
1

I가이 :장고 - 피스톤 방법은 허용되지 않습니다

handlers.py

class ChatHandler(BaseHandler): 
    model = ChatMsg 
    allowed_methods = ('POST','GET') 

    def create(self, request, text): 
     message = ChatMsg(user = request.user, text = request.POST['text']) 
     message.save() 
     return message 

template.html

... 
    <script type="text/javascript"> 
    $(function(){ 
     $('.chat-btn').click(function(){ 
      $.ajax({ 
       url: '/api/post/', 
       type: 'POST', 
       dataType: 'application/json', 
       data: {text: $('.chat').val()} 
      }) 
     }) 
    }) 
    </script> 
    ... 

API/urls.py

chat_handler = Resource(ChatHandler, authentication=HttpBasicAuthentication) 
urlpatterns = patterns('', 
    .... 
    url(r'^chat/$', chat_handler, {'emitter_format': 'json'}), 
) 

왜 ChatHandler에서는 POST 메서드가 허용됩니까? GET 방법이 효과적입니다. 버그입니까, 아니면 코드가 잘못 되었습니까? 당신이 당신의 질문에이 코드를 포함하지 않았다

+1

당신의 URL 패턴은 매개 변수를 생성하기위한 매개 변수를 캡처하지 않습니다. 그것에 충돌하지 않습니까? 아니면 정확한 예가 아닙니까? – jdi

+0

크래시가 발생하지 않습니다. 오류 405가 firebug 콘솔에서 허용되지 않습니다. –

+0

'ChatHandler'가 url'api/chat'으로 라우트되었지만 javascript가'/ api/post /'로 POSTing되는 이유는 무엇입니까? 해당 URL 끝점에서 라우팅되는 항목은 무엇입니까? – jdi

답변

1

에도 불구하고, 나는 당신이 POST는

를 요청 허용하지 않는 처리기에 매핑하는 당신 당신이 의견에 준 GitHub의 ...

에서 그것을 발견 urls.py :: 싹둑 ::

post_handler = Resource(PostHandler, authentication=auth) 
chat_handler = Resource(ChatHandler, authentication=auth) 
urlpatterns = patterns('', 
    url(r'^post/$', post_handler , { 'emitter_format': 'json' }), 
    url(r'^chat/$', chat_handler, {'emitter_format': 'json'}), 
) 

handlers.py

# url: '/api/post' 
# This has no `create` method and you are not 
# allowing POST methods, so it will fail if you make a POST 
# request to this url 
class PostHandler(BaseHandler): 
    class Meta: 
     model = Post 

    # refuses POST requests to '/api/post' 
    allowed_methods = ('GET',) 


    def read(self, request): 
     ... 

# ur;: '/api/chat' 
# This handler does have both a POST and GET method allowed. 
# But your javascript is not sending a request here at all. 
class ChatHandler(BaseHandler): 
    class Meta: 
     model = ChatMsg 
    allowed_methods = ('POST', 'GET') 

    def create(self, request, text): 
     ... 

    def read(self, request): 
     ... 

PostHandler으로 라우팅되는 '/api/post/'으로 자바 스크립트 요청이 전송되었습니다. 당신이 채팅 처리기에 갈 것으로 예상한다는 생각이 들었습니다. 즉, 자바 스크립트 요청 URL을 '/api/chat/'으로 변경하거나 코드를 원하는 핸들러로 옮겨야합니다.

또한 핸들러에서 모델을 잘못 설정했습니다. 중첩 클래스 인 Meta이 필요하지 않습니다. 그것은되어야합니다 :

class PostHandler(BaseHandler): 
    model = Post 

class ChatHandler(BaseHandler): 
    model = ChatMsg 
+0

이지만 GET 메서드가이 처리기에서 작동합니다. js의 url은 main urls.py에 포함되어 있기 때문에 사실입니다. url (r '^ post/$', post_handler, { 'emitter_format': 'json'}) 'url (r'api /', include ('api.urls'))'및'urlpatterns = patterns ('', ), url (r '^ chat/$', chat_handler, { 'emitter_format': 'json'}), ) '이 작업을 수행 한 후'/ api/chat '에 올리기가 가능하지만 Post 메소드가 작동하지 않습니다. –

+0

내가하는 말을 오해하고있는 것 같아. 'PostHandler'에 정의 된 ** NO ** POST 메소드가 있습니다. 그리고 당신은 url'/ api/post /'에 POST 요청을하려고합니다. 여기서 무엇이 잘못되었는지는 분명합니다. – jdi

+0

채팅/내가 API /에서'AttributeError을 가지고, 내가 잘못,하지만 POST 요청이 전달됩니다있을 수 있습니다/ 'HttpResponseServerError'개체가 어떤 속성이 ' –

관련 문제