Django: 对象没有属性 'post'
Django: object has no attribute 'post'
我试图在 Django 中执行 Ajax POST 请求,但它给我一个错误。我对 Ajax GET 请求也有类似的看法,并且效果很好。
这是我的观点:
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'
这里有什么问题?
根据您的堆栈跟踪,我相信您正在使用 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
我试图在 Django 中执行 Ajax POST 请求,但它给我一个错误。我对 Ajax GET 请求也有类似的看法,并且效果很好。 这是我的观点:
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'
这里有什么问题?
根据您的堆栈跟踪,我相信您正在使用 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