def create_task(request): json_value = {} module_code = "04" module_status = "" if request.method == 'POST': if request.user.is_authenticated(): if all(key in request.POST for key in ('name', 'description', 'priority', 'state', 'due_date')): name = request.POST['name'] description = request.POST['description'] priority = request.POST['priority'] state = request.POST['state'] due_date = request.POST['due_date'] if priority and state and name and description and due_date: if 1 <= int(priority) <= 5 and 1 <= int(state) <= 3: task_object = Tasks() task_object.name = name task_object.description = description task_object.priority = priority task_object.state = state task_object.due_date = due_date task_object.user = request.user try: task_object.save() module_status = "01" # success except ValidationError: module_status = "00" # post data; date field format not correct yyyy-mm-dd else: module_status = "00" # post data error else: module_status = "00" # post data error else: module_status = "00" # post data error else: module_status = "02" # user not logged in else: module_status = "00" # method not post json_value['status_code'] = module_code + module_status return_value = json.dumps(json_value) return HttpResponse(return_value)