2016-01-12 139 views
0

我試圖在Django中執行Ajax POST請求,但它給了我一個錯誤。我對Ajax GET請求也有類似的看法,並且效果很好。 這是我的看法:Django:object has no attribute'post'

class ChangeWordStatus(JSONResponseMixin, AjaxResponseMixin, View): 

def get_ajax(self, request, *args, **kwargs): 
    user_profile = get_object_or_404(UserProfile, user=request.user) 
    lemma = request.POST['lemma'] 
    new_status_code = int(request.POST['new_status_code']) 

    word = Word.objects.get(lemma=lemma) 
    old_status_code = user_profile.get_word_status_code(word) 

    json_dict = {} 
    json_dict["message"] = "Done" 

    return self.render_json_response(json_dict) 

我得到這個:

Traceback: 
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response 
    132.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in view 
    71.    return self.dispatch(request, *args, **kwargs) 
File "C:\Python34\lib\site-packages\braces\views\_ajax.py" in dispatch 
    78.    return handler(request, *args, **kwargs) 
File "C:\Python34\lib\site-packages\braces\views\_ajax.py" in post_ajax 
    87.   return self.post(request, *args, **kwargs) 

Exception Type: AttributeError at /slv/change_word_status/ 
Exception Value: 'ChangeWordStatus' object has no attribute 'post' 
Request information: 
GET: No GET data 

POST: 
csrfmiddlewaretoken = 'eapny7IKVzwWfXtZlmo4ah657y6MoBms' 
lemma = 'money' 
new_status_code = '1' 

這裏有什麼問題?

+0

你需要一個'。員額()'方法來捕捉'POST'請求​​。 – Gocht

回答

2

從你的堆棧跟蹤中,我相信你正在使用django-braces

您發送POST請求,但您沒有post_ajax方法。我假設你的get_ajax函數實際上應該是post_ajax

class ChangeWordStatus(JSONResponseMixin, AjaxResponseMixin, View): 

    def post_ajax(self, request, *args, **kwargs): 
     user_profile = get_object_or_404(UserProfile, user=request.user) 
     lemma = request.POST['lemma'] 
     new_status_code = int(request.POST['new_status_code']) 

     word = Word.objects.get(lemma=lemma) 
     old_status_code = user_profile.get_word_status_code(word) 

     json_dict = {} 
     json_dict["message"] = "Done" 

     return self.render_json_response(json_dict) 

參考:https://django-braces.readthedocs.org/en/latest/other.html#ajaxresponsemixin

+0

謝謝,它很明顯,但我沒有注意到很長一段時間。 – hopheylalaley

相關問題