def question_image_create_api(request, question_id): # Get question to which the image will be attached. try: question = Question.objects.get(id=question_id) except: raise Http404 # Save photo if POST if request.method == 'POST': form = QuestionImageForm(request.POST, request.FILES) if form.is_valid(): # Create new question image and store in DB new_question_image = QuestionImage() new_question_image.image = request.FILES['question_image'] new_question_image.question = question new_question_image.save() # if request.FILES.has_key('causeandeffect_image'): # return HttpResponse('its there', mimetype="text/plain") # else: # return HttpResponse('nope', mimetype="text/plain") # Serialize questions in JSON format # i.e., https://docs.djangoproject.com/en/dev/topics/serialization/ serialized_question_image = serializers.serialize('json', [new_question_image], fields=('text')) return HttpResponse(serialized_question_image, mimetype="application/json") else: raise Http500
def question_image_create(request, question_id): # Get question to which the image will be attached. try: question = Question.objects.get(id=question_id) except: raise Http404 # Save photo if POST if request.method == 'POST': form = QuestionImageForm(request.POST, request.FILES) if form.is_valid(): # Create new question image and store in DB new_question_image = QuestionImage(image = request.FILES['question_image']) new_question_image.question = question new_question_image.save() return HttpResponseRedirect(reverse('sinq3.views.question_view', args=(question.id,))) else: form = QuestionImageForm() # An empty, unbound form return render_to_response( 'question_images/create.html', { 'question': question, 'form': form }, context_instance=RequestContext(request) )