Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Django的使用记录_2

$
0
0

=Start=

缘由:

记录在使用Django的过程中碰到的各种问题,方便以后参考。

正文: 1.碰到「CSRF verification failed. Request aborted.」怎么办? $.ajax({ data: { somedata: 'somedata', moredata: 'moredata', csrfmiddlewaretoken: '{{ csrf_token }}' }, ... }) // http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request

&

<formaction="" method="post">{% csrf_token %} // https://docs.djangoproject.com/en/1.8/ref/csrf/#how-to-use-it 2.在Django应用中如何返回json数据? response_data = {} response_data['result'] = 'error' response_data['message'] = 'Some error message'

在 Django 1.7 之前的版本中,可以使用下面的方法:

importjson fromdjango.httpimportHttpResponse return HttpResponse(json.dumps(response_data), content_type="application/json")

对于 Django 1.7 之后的版本,可以使用Django官方提供的方法:

fromdjango.httpimport JsonResponse return JsonResponse({'foo':'bar'}) https://docs.djangoproject.com/en/1.10/ref/request-response/#jsonresponse-objects https://docs.djangoproject.com/en/1.10/ref/request-response/#id4 http://stackoverflow.com/questions/2428092/creating-a-json-response-using-django-and-python http://www.ziqiangxuetang.com/django/django-ajax.html 3.待添加

……

=END=


Viewing all articles
Browse latest Browse all 9596

Trending Articles