def test_can_save_question_to_database(self): first_question = Question() first_question.text = '1 + 1 = 2' first_question.ans = 'True' first_question.save() saved_items = Question.objects.all() self.assertEqual(saved_items.count(), 1) second_question = Question() second_question.text = '1 = 2' second_question.ans = 'False' second_question.save() saved_items = Question.objects.all() self.assertEqual(saved_items.count(), 2) self.assertEqual(Question.objects.all()[0].text, '1 + 1 = 2') self.assertEqual(Question.objects.all()[1].text, '1 = 2')
def home_page(request): if request.method == 'POST': q = Question() question_text = request.POST.get('question_text', '') ans = request.POST.get('ans', '') q.text = question_text q.ans = ans q.save() return redirect('/') questions = Question.objects.all() return render(request, 'quiz/home.html', {'questions': questions})
def home_page(request): if request.method == 'POST': q = Question() question_text = request.POST.get('question_text','') ans = request.POST.get('ans','') q.text = question_text q.ans = ans q.save() return redirect('/') questions = Question.objects.all() return render(request, 'quiz/home.html', {'questions':questions})