I am building a chatbot using Django that can take survey's preset answer choices and give different scores to different answer choices. After that, the chatbot will sum all the scores accordingly and print out the results.
This is a sample question with preset answer choices
<select name="survey1-q" data-conv-question="Bạn có hay nghĩ về một điều sẽ xảy ra trong tương lai theo hướng tồi tệ, thậm chí rất tiêu cực?">
<option value="survey1-never">Không bao giờ</option>
<option value="survey1-rarely">Hiếm khi</option>
<option value="survey1-sometimes">Đôi khi</option>
<option value="survey1-often">Thường xuyên</option>
<option value="survey1-veryoften">Rất thường xuyên</option>
</select>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
This is my if-statement inside a for loop
<!--looping and getting the survey's result-->
{% for i in survey1-q%}
{% if survey1-q is "Không bao giờ"%}
{{score}}={{score 0}}
{% elif survey1-q is "Hiếm khi" %}
{{score}}={{score 1}}
{%elif survey1-q is "Đôi khi"%}
{{score}}={{score 2}}
{%elif survey1-q is "Thường xuyên"%}
{{score}}={{score 3}}
{%elif survey1-q is "Rất thường xuyên"%}
{{score}}={{score 4}}
{%endif%}
{% endfor %}
<p>Điểm của bạn là {{score}}</p>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
However, after the survey's questions are finished, the chatbot automatically load back to the beginning stage and ask the first question instead of printing the {{score}}
I am afraid I had been wrong in calling out the variables in the for loop and if statement but after researching, I still couldn't figure it out. Please help me! Thank you!
CodePudding user response:
A HTML page cannot store a variable in between page refreshes. Django templating language uses variables but they are resetted when your form is validated and thus when the page is reloaded from backend.
The logic should be on the backend side. Your form should send the result to the backend (your Django view view.py
) where the score can be calculated, stored and/or retrieved in Python. The output of the view the output can then render the template along with the score as a template variable.
So, assuming that your HTML select
is part of a HTML form sending back the input as GET parameter named survey_response
, your Python view view.py
would look like something like that:
from django.shortcuts import render
from django.http import HttpResponse
def your_view(request) -> HttpResponse:
if not score:
score: int = 0
survey_response: str = request.GET.get('survey_response')
if survey_response == "Hiếm khi":
score = 1
elif survey_response == "Đôi khi":
score = 2
elif survey_response == "Thường xuyên":
score = 3
elif survey_response == "Rất thường xuyên":
score = 4
return render(
request,
"your_template.html",
{
"score": score
},
)
Then on the template's side (your_template.html
), you don't need any loop neither any logic, the only thing is to render the score with <p>Điểm của bạn là {{score}}</p>
.