我的iOS应用需要向Django发送POST请求。 我使用“@ csrf_exempt”来禁用“CSRF”。 在本地,iOS和Django运行良好。 但是在我将网站部署到“webfaction”之后,Django无法获得iOS POST请求。 我的问题是如何处理有关CSRF的这个问题,我需要对Django做一些其他的设置吗?
[这是我的Django代码]
__author__ = 'zhaonanli'
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
import json
from models import *
import umpme.settings
import base64
@csrf_exempt
def upload_image_for_profile(request):
static_dir = umpme.settings.STATIC_ROOT
users_dir = static_dir + 'users/'
back_dic = {}
if request.method == 'POST':
if request.raw_post_data:
try:
json_data_decoding = json.loads(request.raw_post_data)
big_image_raw_data_b64string = json_data_decoding['big_imageb64string']
small_image_raw_data_b64string = json_data_decoding['small_imageb64string']
big_image_raw_data = base64.b64decode(big_image_raw_data_b64string)
small_image_raw_data = base64.b64decode(small_image_raw_data_b64string)
uid = json_data_decoding['uid']
user = UserAccountInfo.objects.get(id=uid)
uemail = user.usignup_email
whole_image_dir = users_dir + 'umpme_' + uemail + '_user/'
bigimage_filename = whole_image_dir + 'bigprofile.jpg'
smallimage_filename = whole_image_dir + 'smallprofile.jpg'
bigimage = open(bigimage_filename, 'wb')
bigimage.write(big_image_raw_data)
bigimage.close()
smallimage = open(smallimage_filename, 'wb')
smallimage.write(small_image_raw_data)
smallimage.close()
back_dic['succ'] = 'yes'
back_dic['error'] = 'nothing'
back_dic['uid'] = uid
except Exception, e:
back_dic['succ'] = 'no'
back_dic['error'] = 'error = ' + str(e)
back_dic['uid'] = 'none'
else:
back_dic['succ'] = 'no'
back_dic['error'] = 'cannotgetrawpostdata'
back_dic['uid'] = 'none'
else:
back_dic['succ'] = 'no'
back_dic['error'] = 'notgetrequest'
back_dic['uid'] = 'none'
return HttpResponse(json.dumps(back_dic), content_type='application/json')