我认为ajax数据放在请求体中。在
试试看你有没有拿到你的数据from django.http import QueryDict
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def validate_username(request):
if request.method == "POST" and request.is_ajax():
# The body of the request is in byte form,
# so we decode/convert to string using utf-8 encoding
datastring = request.body.decode("utf-8")
# The data comes in as if it was coming
# from a real form i.e. each field on the model
# comes separated by & e.g. the_post=demo&othefield=4 etc
# use djangos QueryDict to get a dictionary you can easily
# work with.
datadictionary = QueryDict(datastring)
# retrieve value like any other dictionary
description = datadictionary.get("the_post")
projects = Project.objects.filter(user__id__icontains = request.user.id)\
.filter(pro_description__icontains = description)\
.values_list('pro_description')
response_data = {}
try:
response_data['result'] = 'Success'
response_data['message'] = list(projects)
except:
response_data['result'] = 'Unsuccessful'
response_data['message'] = 'The Subprocess module did not run the script correctly.'
return HttpResponse(json.dumps(response_data), content_type="application/json")