1

I have this:

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'}),
)

Why, but POST method is allowed in ChatHandler? GET method is working. Is it bug, or my code is wrong?

LiGhT_WoLF
  • 71
  • 10
  • 1
    Your url pattern doesnt capture a param to pass to the create methods text parameter. Isnt it crashing on that or is this not an accurate example? – jdi Sep 18 '12 at 17:47
  • it is not crashing, i have error 405 method is not allowed in firebug's console – LiGhT_WoLF Sep 18 '12 at 17:54
  • Why is your `ChatHandler` routed to url `api/chat` but your javascript is POSTing to `/api/post/`? What is routed at that url endpoint? – jdi Sep 18 '12 at 18:19
  • this code is a short version, api/urls.py included in general urls.py url(r'api/', include('api.urls')), – LiGhT_WoLF Sep 18 '12 at 18:24
  • I understand. But your examples here dont match. What is on the other end of api/post ? What handler? – jdi Sep 18 '12 at 18:34
  • you can see my full example in [GITHUB](https://github.com/lightwolf03/prog) in api/* and in templates – LiGhT_WoLF Sep 19 '12 at 09:16
  • It was exactly as I expected, now that you showed your code. See my answer. – jdi Sep 19 '12 at 14:50

1 Answers1

1

Even though you didn't include this code in your question, I found it in your github you gave in comments...

You are mapping to a handler that does not allow POST requests

urls.py ::snip::

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):
        ...

Your javascript requests is being sent to '/api/post/', which is being routed to the PostHandler. I get the sense that you expected it to go to the chat handler. That means you need to change the javascript request url to '/api/chat/', or move the code to the handler you expected.

Also, you are setting the model wrong in the handlers. It does not need a Meta nested class. It should be:

class PostHandler(BaseHandler):
    model = Post

class ChatHandler(BaseHandler):
    model = ChatMsg
jdi
  • 90,542
  • 19
  • 167
  • 203
  • but GET method is working in this handler. url in js is true, because it included in main urls.py. `url(r'api/', include('api.urls'))` and `urlpatterns = patterns('', url(r'^post/$', post_handler , { 'emitter_format': 'json' }), url(r'^chat/$', chat_handler, {'emitter_format': 'json'}), )` after this i have working Get on '/api/chat', but Post method not working – LiGhT_WoLF Sep 19 '12 at 16:27
  • I think you are misunderstanding what I am saying. You have **NO** POST method defined or allowed on the `PostHandler`. And you are trying to make a POST request to the url `/api/post/`. It is very clear here what is wrong. – jdi Sep 19 '12 at 17:26
  • there can be I am mistaken, but post request is passed, i have `AttributeError at /api/chat/ 'HttpResponseServerError' object has no attribute '_is_string'` – LiGhT_WoLF Sep 19 '12 at 19:22
  • Are you saying that you changed your javascript url to `/api/chat` and it is now making the request but getting an error? – jdi Sep 19 '12 at 19:25
  • sorry for my english, you can misunderstand me. browser send POST request to handler, process enter in handler but on the creating object "message" i have error `AttributeError at /api/chat/ 'HttpResponseServerError' object has no attribute '_is_string'` – LiGhT_WoLF Sep 20 '12 at 04:15
  • Well this problem seems unrelated to your original question. It is probably [this logged Issue #221](https://bitbucket.org/jespern/django-piston/issue/221/attributeerror-httpresponseservererror) which would occur if you are using django 1.4 in combination with unicode content instead of string content – jdi Sep 20 '12 at 13:30
  • well, am i need to patch piston? – LiGhT_WoLF Sep 20 '12 at 15:02
  • If you are indeed using the same setup as listed in that issue...then I would assume so. – jdi Sep 20 '12 at 17:14