def question_create_api(request): if request.method == 'POST': question_json = request.body json_data = simplejson.loads(question_json) try: #data = json_data[0] question_data = json_data['question'] question = Question(text = question_data['text']) question.save() if question_data.has_key('causeandeffect_id'): causeandeffect = CauseAndEffect.objects.get(id=question_data['causeandeffect_id']) if causeandeffect: question.causeandeffects.add(causeandeffect) if question_data.has_key('investigation_id'): investigation = Investigation.objects.get(id=question_data['investigation_id']) if investigation: question.investigations.add(investigation) # Serialize questions in JSON format # i.e., https://docs.djangoproject.com/en/dev/topics/serialization/ serialized_questions = serializers.serialize('json', [question], fields=('text')) return HttpResponse(serialized_questions, mimetype="application/json") except KeyError: HttpResponseServerError("Malformed data!") return HttpResponse(object) else: HttpResponseServerError("Not POST. Must POST to this URL.")
def question_create(request): if request.method == 'POST': form = QuestionForm(request.POST, request.FILES) if form.is_valid(): # Create new question image and store in DB question_text = request.POST['question_text'] question = Question(text = question_text) question.save() return HttpResponseRedirect(reverse('sinq3.views.question_view', args=(question.id,))) else: form = QuestionForm() # An empty, unbound form return render_to_response( 'questions/question_create.html', { 'form': form }, context_instance=RequestContext(request) )