Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django
#1
Hi all
I am doing a multiplication tables quiz project in Django. Everything will correct . But the only issue is at quiz I did not get entered value in quiz
In views.py
def multiplication_quiz(request):
table_number = None
if request.method == 'POST':
table_number = int(request.POST.get('table_number', 0))
score = 0
questions = []
question_numbers = range(1, 11)
for i in question_numbers:
num1 = table_number
num2 = int(request.POST.get('answer{}'.format(i), 0))
is_correct = num2 == num1 * i
if is_correct:
score += 1
questions.append({'question_number': i, 'num1': num1, 'num2': num2, 'correct': is_correct})
return render(request, 'quiz_result.html', {'score': score, 'questions': questions, 'table_number': table_number})
return render(request, 'quiz.html', {'question_numbers': range(1, 11), 'table_number': table_number})

in quiz.html
{% extends 'base.html' %}
{% block start %}
<h1>This for quiz 1 X 1 = ?</h1>
<form method="post">
{% csrf_token %}
<label for="table_number">Enter the number for multiplication table:</label>
<input type="number" name="table_number" id="table_number" required><br><br>

{% for i in question_numbers %}
<label for="answer{{ i }}">Question {{ i }}: What is {{ table_number }} times {{ i }}?</label>
<input type="number" name="answer{{ i }}" id="answer{{ i }}" required><br>
{% endfor %}

<button type="submit">Submit</button>
</form>
{% endblock %}

output
Enter the number for multiplication table:
2


Question 1: What is None times 1?
2

Question 2: What is None times 2?
4

Question 3: What is None times 3?
6

Question 4: What is None times 4?
8

Question 5: What is None times 5?
10

Question 6: What is None times 6?
12

Question 7: What is None times 7?
14

Question 8: What is None times 8?
16

Question 9: What is None times 9?
18

Question 10: What is None times 10?
20

Submit

my doubt : I don't want to show None in Questions . I want entered value in place of None
what should I do to get Output like this Question 1: What is 2 times 1?
Please help me
Larz60+ write Apr-14-2024, 09:52 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020