Hi all,
Can someone please help me to understand what wrong is with the code below? Actually it is working, but if I try to render the second db QuesAnswer searching for/by question, it is not displaying or throwing an error. Although by debugging or using shell I see there is the expected output to display on front-end.
Here is my view.py
class MainPageView(TemplateView):
model= Definitions
# template_name = "encyclopedia/draft.html"
def get(self, request):
# Get inputs value
queryOne = request.GET.get('QuizOne')
queryTwo = request.GET.get('QuizTwo')
queryThree = request.GET.get('QuizThree')
#
if queryOne:
def_output = Definitions.objects.filter(
Q(term__icontains=queryOne) | Q(term__contains=queryOne)
)
ques_output = QuesAnswer.objects.values().filter(
Q(question__icontains=queryOne) | Q(question__contains=queryOne)
)
# print(ques_output)
return render(request, 'encyclopedia/draft.html', {'def_object': def_output, "ques_object": ques_output})
elif queryTwo:
def_output = Definitions.objects.filter(
Q(term__icontains=queryTwo) | Q(term__contains=queryTwo)
)
ques_output = QuesAnswer.objects.values().filter(
Q(question__icontains=queryTwo) | Q(question__contains=queryTwo)
)
# print(ques_output)
return render(request, 'encyclopedia/draft.html', {'def_object2': def_output, "ques_object2": ques_output})
elif queryThree:
def_output = Definitions.objects.filter(
Q(term__icontains=queryThree) | Q(term__contains=queryThree)
)
ques_output = QuesAnswer.objects.values().filter(
Q(question__icontains=queryThree) | Q(question__contains=queryThree)
)
# print(ques_output)
return render(request, 'encyclopedia/draft.html', {'def_object3': def_output, "ques_object3": ques_output})
return render(request, 'encyclopedia/draft.html', {})
Models.py
class Definitions(models.Model):
# user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
term = models.CharField(null=False, blank=False, max_length=10, validators=[MinLengthValidator(limit_value=2, message="Hehe, the minimum required character is 2. Look at the example.")])
definition = models.CharField(null=False, blank=False, max_length=1500, validators=[MinLengthValidator(limit_value=100, message="Term defition should contain minimum 100 characters.")])
def __str__(self) -> str:
return self.term
class QuesAnswer(models.Model):
# user = models.ForeignKey(settings.AUTH_USER_MODEl, on_delete=models.CASCADE)
question = models.CharField(max_length=500, null=False, blank=False)
A="don't know"
B='need time'
C='reading book'
D='any answer'
QUESTION_CHOICE = [
("select", 'Choose one...'),
(A, "Don\'t know"),
(B, 'Need time'),
(C, 'Reading book'),
(D, 'Any answer')]
choices = models.CharField(
max_length=100, choices=QUESTION_CHOICE, blank=True, default='select')
answer = models.CharField(max_length=1, null=False, blank=False)
explanation = models.TextField()
def __str__(self) -> str:
return self.question
draft.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="term_search">
<div class="srch" id="srch">
<h3 class="h3">Quick search</h3>
<div class="srch_input">
<form action="{% url 'encyclopedia:index' %}" method="get" class="fsearch" id="formOne">
{% csrf_token %}
<i>for question/term:</i>
<input
type="text"
name="QuizOne"
placeholder="e.g iod or what is battery?"
class="w-50 rounded"
minlength="2"
size="150"
maxlength="150"
required
/>
<button id="btnOne" class="btn btn-info">Find</button>
<!-- default name for the context in ListView -->
{% for word in def_object %}
{% for qword in ques_object %}
{% if word.term %}
<div class="srch_output" >
<div>
{{ word.id }}. {{ word.term }} <br />
Definition: {{ word.definition }}
</div>
</div>
{% if qword.question %}
<div class="srch_output" >
<div>
{{ qword.id }}. {{ qword.question }} <br />
Answer choice: {{ qword.answer}} - {{ qword.choices }} <br />
Explanation: {{ qword.explanation}}
</div>
</div>
{% else %}
<div class="srch_output" >
<div>
Please search for a term to get definition or for a question to get answer and right choice to understand.
</div>
</div>
{% endif %}
{% else %}
<div class="srch_output" >
<div>
Please search for a term to get definition or for a question to get answer and right choice to understand.
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</form>
</div>
</div>
</div>
<div class="term_search">
<div class="srch" id="srch">
<h3 class="h3">Quick search</h3>
<div class="srch_input">
<form action="{% url 'encyclopedia:index' %}" method="get" class="fsearch" id="formTwo">
{% csrf_token %}
<i>for question/term:</i>
<input
type="text"
name="QuizTwo"
placeholder="e.g ip or what is ip?"
class="w-50 rounded"
minlength="2"
size="150"
maxlength="150"
required
/>
<button id="btnTwo" class="btn btn-info">Find</button>
<!-- default name for the context in ListView -->
{% for iod in def_object2 %}
{% for qiod in ques_object2 %}
{% if word.term %}
<div class="srch_output" >
<div>
{{ iod.id }}. {{ iod.term }} <br />
Definition: {{ iod.definition }}
</div>
</div>
{% if qiod.question %}
<div class="srch_output" >
<div>
{{ qiod.id }}. {{ qiod.question }} <br />
Answer choice: {{ qiod.answer}} - {{ qiod.choices }} <br />
Explanation: {{ qiod.explanation}}
</div>
</div>
{% else %}
<div class="srch_output" >
<div>
Please search for a term to get definition or for a question to get answer and right choice to understand.
</div>
</div>
{% endif %}
{% else %}
<div class="srch_output" >
<div>
Please search for a term to get definition or for a question to get answer and right choice to understand.
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</form>
</div>
</div>
</div>
<div class="term_search">
<div class="srch" id="srch">
<h3 class="h3">Quick search</h3>
<div class="srch_input">
<form action="{% url 'encyclopedia:index' %}" method="get" class="fsearch" id="formThree">
{% csrf_token %}
<i>for question/term:</i>
<input
type="text"
name="QuizThree"
placeholder="e.g it or what is it?"
class="w-50 rounded"
minlength="2"
size="150"
maxlength="150"
required
/>
<button id="btnThree" class="btn btn-info">Find</button>
<!-- default name for the context in ListView -->
{% for ict in def_object3 %}
{% for qict in ques_object3 %}
{% if word.term %}
<div class="srch_output" >
<div>
{{ ict.id }}. {{ ict.term }} <br />
Definition: {{ ict.definition }}
</div>
</div>
{% if qict.question %}
<div class="srch_output" >
<div>
{{ qict.id }}. {{ qict.question }} <br />
Answer choice: {{ qict.answer}} - {{ qict.choices }} <br />
Explanation: {{ qict.explanation}}
</div>
</div>
{% else %}
<div class="srch_output" >
<div>
Please search for a term to get definition or for a question to get answer and right choice to understand.
</div>
</div>
{% endif %}
{% else %}
<div class="srch_output" >
<div>
Please search for a term to get definition or for a question to get answer and right choice to understand.
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</form>
</div>
</div>
</div>
</body>
</html>
The only problem is that if I search by question to get the explanation, answer etc, it does not render anything on front-end.
By the way, buttons are disabled here.
Thanks in advvance.
Kind regards,
Kanatbek