當採用客戶端象django的服務器提交post請求時。會獲得403,權限異常。django
由於django針對提交的請教,有校驗。因此會如此。json
解決辦法http://stackoverflow.com/questions/6800894/django-returns-403-error-when-sending-a-post-requestapi
導入模塊服務器
from django.views.decorators.csrf import csrf_exempt函數
在函數前面添加修飾器post
@csrf_exemptcsrf
#客戶端提交的post若是不加這段,會出現403error
@csrf_exempt
def api_blogs(request):
if request.method == 'POST' and request.POST['page']:
int_page = int(request.POST['page'])
else:
int_page = 1blog
blogs = dbBlog.objects.order_by('-created_date').all()get
page_size = 10
after_range_num = 5
before_range_num = 6io
paginator = Paginator(blogs, page_size)
try:
blogs = paginator.page(int_page)
except(EmptyPage, InvalidPage, PageNotAnInteger):
blogs = paginator.page(1)
try:
return_json = serializers.serialize('json',blogs.object_list)
except :
return_json = {
'status': 1,
'msg' '提取blog異常'
}
return HttpResponse( return_json )