Reputation: 283
I cannot show the value in html. I wrote in views.py
def score_test(request):
results = TestScore.objects.filter(user=request.user).all()
print(results)
return render(request, 'score_test.html',{'results': results})
in score_test.html
<div class="col-xs-12">
<h2 class="result_title">Test Score</h2>
<h3>Your score is
{% for result in results.all %}
{% if result.score > 95 %}
<h4 class="one"> {{ result.score }} </h4>
{% else %}
{% if result.score > 80 %}
<h4 class="two"> {{ result.score }} </h4>
{% else %}
{% if result.score > 60 %}
<h4 class="three"> {{ result.score }} </h4>
{% else %}
{% if result.score > 40 %}
<h4 class="four"> {{ result.score }} </h4>
{% else %}
<h4 class="five"> {{ result.score }} </h4>
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</h3>
</div>
in models.py
class TestScore(models.Model):
score = models.FloatField(null=True, blank=True, default=None)
In print(results) of views.py, I got <function tc at 0x105a15d70>
, so I think I can get score of TestScore model in results. So, how can I fix this? Am I wrong to write if-else statement in template of score_test.html?
Upvotes: 0
Views: 64
Reputation: 77912
"function tc at 0x105a15d70" is definitely not what TestCore.objects.filter()
should return (it should be a queryset), and certainly not what the code you posted could yield, so no surprise you don't see anything in your template.
This being said there are a couple possible improvements to your code...
First, in your view:
results = TestScore.objects.filter(user=request.user).all()
the .all()
is redundant. You just need:
results = TestScore.objects.filter(user=request.user)
You could even just use the reverse relation (assuming you didn't specify a related_name
in your TestScore.user
field):
results = request.user.testscore_set.all()
Then in your templates:
{% for result in results.all %}
The ".all" is once again useless, and possibly (or not) the cause of your problem. But well, it's useless so don't use it ;)
{% for result in results %}
Then you could use {% elif <condition> %} instead of nested
{% else %}{% if %}`:
{% if result.score > 95 %}
<h4 class="one"> {{ result.score }} </h4>
{% elif result.score > 80 %}
<h4 class="two"> {{ result.score }} </h4>
{% elif result.score > 60 %}
<h4 class="three"> {{ result.score }} </h4>
{% elif result.score > 40 %}
<h4 class="four"> {{ result.score }} </h4>
{% else %}
<h4 class="five"> {{ result.score }} </h4>
{% endif %}
But using a custom template filter would make your code simpler:
# <yourapp>/templatetags/<yourlibname>.py
from django import template
register = template.Library()
@register.filter
def score_grade(score):
try:
score = int(score)
except (TypeError, ValueError):
# XXX should at least log the exception
return ""
if score > 95:
return "one"
elif score > 80:
return "two"
elif score > 60:
return "three"
elif score > 40:
return "four"
else:
return "five"
and in your template:
{% load <yourlibname> %}
{% for result in results %}
<h4 class="{{ result.score|score_grade }}"> {{ result.score }} </h4>
{% endfor %}
Upvotes: 1
Reputation: 504
You can use elif command.
{% if result.score > 95 %}
<h4 class="one"> {{ result.score }} </h4>
{% elif result.score > 80 %}
<h4 class="two"> {{ result.score }} </h4>
...
{% else %}
<h4 class="five"> {{ result.score }} </h4>
{% endif %}
Upvotes: 0