def question(request, id): """ For showing questions and posting answer """ question = get_object_or_404(Question, id=id) context = {} context['question'] = question if request.method == 'GET': form = AnswerForm() context['form'] = form if request.method == 'POST': form = AnswerForm(request.POST or None) if form.is_valid(): answer = form.save(commit=False) answer.created_by = request.user answer.question = question form.save() form = AnswerForm() context['form'] = form return render(request, 'core/question.html', context=context)
def postAnswer(request): if request.method != "POST": return HttpResponse("Must post.") elif not request.user.is_authenticated(): return HttpResponse("not authenticated") answer = Answer() answer.user = request.user answerForm = AnswerForm(request.POST, instance=answer) answerForm.save() return HttpResponse("answer saved")
def answer(request): if request.method == 'POST': answer_form = AnswerForm(request.POST) if answer_form.is_valid(): answer = answer_form.save(commit=False) answer.save() answer.question.answered = True answer.question.save() return HttpResponse(json.dumps({'status': 'ok'})) return HttpResponse(json.dumps({'status': 'error'}))
def home(request): form = AnswerForm() datas = AnswerTable.objects.all() import os print(os.path) if request.method == 'POST': form = AnswerForm(request.POST, request.FILES) if form.is_valid(): data = AnswerTable() data.student_name = request.POST['student_name'] data.subject_name = request.POST['subject_name'] data.answer = request.FILES['answer'] data.answer_key = request.FILES['answer_key'] data.marks = random.randrange(100) # dummy random marks data.save() return redirect('home') return render(request, 'home.html', {'form': form, 'datas': datas})
def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(QuestionDetailView, self).get_context_data(**kwargs) context['answers'] = Answer.objects.filter( question_id=self.object.pk).all() context['answerForm'] = AnswerForm( initial={'question_id': self.object.pk}) return context
def answer_add(request,qid): """ add answer """ question = get_object_or_404(Question,id=qid) if request.method == 'POST': form = AnswerForm(request.user,question,request.POST) if form.is_valid(): form.save() request.user.message_set.create(message="Thanks for answer the question") return HttpResponseRedirect(reverse('core-question',args=[question.id])) else: form = AnswerForm(request.user,question) return render_to_response("core/answer.html", { 'form' : form, 'question':question },context_instance=RequestContext(request))
def index(request, lang): template_name = 'core/{}/index.html'.format(lang if lang else 'ru') participant_id = request.session.get('participant_id') participant = get_or_none(Participant, pk=participant_id) participant_form = ParticipantForm() questions = Question.objects.filter(participant=participant, answered=False) forms = [] for question in questions: forms.append({ 'question': question, 'answer_left_form': AnswerForm(initial={ 'best': question.left, 'question': question }), 'answer_right_form': AnswerForm(initial={ 'best': question.right, 'question': question }), 'answer_none_form': AnswerForm(initial={ 'best': None, 'question': question }), }) return render( request, template_name, { 'video_path': settings.VIDEO_CORE_PATH, 'participant': participant, 'questions': questions, 'participant_form': participant_form, 'forms': forms, 'lang': lang, })
def answer_question(request, pk): answer = Answer.objects.all() if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.save() return redirect('home') else: form = AnswerForm() return render(request, 'core/answer.html', { 'form': form, 'answer': answer })
def add_answer(request, pk): print('question') from core.forms import AnswerForm from django.views.generic.edit import CreateView # answer = get_object_or_404(Question, pk=pk) # question = get_object_or_404(Question, pk=pk) if request.method == "POST": form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.user = request.user answer.target_question = get_object_or_404(Question, pk=pk) form.save() # send_mail( # f'{answer.user} answered your question', # f"{answer.user} posted an answer to your question: {question}: '{answer}''", # '*****@*****.**', # [f'{question.owner.email}'], # fail_silently=False, # ) else: form = AnswerForm() return HttpResponse()
def edit_answer(request, pk): context = {} qu = BookNode.objects.get(pk=pk) context['module'] = qu.get_book().module context['book'] = qu.get_book() context['question'] = qu context['subtree'] = qu.get_descendants(include_self=True) context['chapter'] = qu.get_parent_chapter() assignment = qu.get_parent_assignment() context['assignment'] = assignment context['toc'] = qu.get_siblings(include_self=True) # navigation questions = assignment.get_descendants().filter(node_type="question").order_by('mpath') next = questions.filter( mpath__gt=qu.mpath ) prev = questions.filter( mpath__lt=qu.mpath ).order_by('-pk') context['next'] = next[0] if next else None context['prev'] = prev[0] if prev else None # answer form # retreive current saved answer (if any) try: ans = Answer.objects.get(question=qu, user=request.user) form = AnswerForm(instance=ans) except Answer.DoesNotExist: form = AnswerForm(initial={'question': qu, 'user': request.user}) # gulp if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): ques = form.cleaned_data['question'] user = form.cleaned_data['user'] # search for saved answer answer = Answer.objects.filter(question=ques, user=user).first() # create new answer if required if not answer: answer = form.save(commit=False) answer.save() # switch on button pressed if 'save-answer' in request.POST: answer.text = form.cleaned_data['text'] answer.is_readonly = form.cleaned_data['is_readonly'] answer.save() elif 'save-and-exit' in request.POST: answer.text = form.cleaned_data['text'] answer.is_readonly = form.cleaned_data['is_readonly'] answer.save() return HttpResponseRedirect( reverse('homework', kwargs={'pk': ques.get_parent_assignment().id}) ) elif 'exit' in request.POST: return HttpResponseRedirect( reverse('homework', kwargs={'pk': ques.get_parent_assignment().id}) ) else: print "BAD LUCK" else: print "FORM INVALID" context['debug'] = form.errors context['form'] = form return render(request, 'edit_answer.html', context)
def edit_answer(request, pk): context = {} qu = BookNode.objects.get(pk=pk) context['module'] = qu.get_book().module context['book'] = qu.get_book() context['question'] = qu context['subtree'] = qu.get_descendants(include_self=True) context['chapter'] = qu.get_parent_chapter() assignment = qu.get_parent_assignment() context['assignment'] = assignment context['toc'] = qu.get_siblings(include_self=True) # navigation questions = assignment.get_descendants().filter( node_type="question").order_by('mpath') next = questions.filter(mpath__gt=qu.mpath) prev = questions.filter(mpath__lt=qu.mpath).order_by('-pk') context['next'] = next[0] if next else None context['prev'] = prev[0] if prev else None # answer form # retreive current saved answer (if any) try: ans = Answer.objects.get(question=qu, user=request.user) form = AnswerForm(instance=ans) except Answer.DoesNotExist: form = AnswerForm(initial={'question': qu, 'user': request.user}) # gulp if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): ques = form.cleaned_data['question'] user = form.cleaned_data['user'] # search for saved answer answer = Answer.objects.filter(question=ques, user=user).first() # create new answer if required if not answer: answer = form.save(commit=False) answer.save() # switch on button pressed if 'save-answer' in request.POST: answer.text = form.cleaned_data['text'] answer.is_readonly = form.cleaned_data['is_readonly'] answer.save() elif 'save-and-exit' in request.POST: answer.text = form.cleaned_data['text'] answer.is_readonly = form.cleaned_data['is_readonly'] answer.save() return HttpResponseRedirect( reverse('homework', kwargs={'pk': ques.get_parent_assignment().id})) elif 'exit' in request.POST: return HttpResponseRedirect( reverse('homework', kwargs={'pk': ques.get_parent_assignment().id})) else: print "BAD LUCK" else: print "FORM INVALID" context['debug'] = form.errors context['form'] = form return render(request, 'edit_answer.html', context)