i have the following template namely index.html.I am working in django framework.Now i have shown the value where the all speed of a car is more than 30km.I just posted here the part of the associated code which output the value,that is,the code is
{% for v in values %}
{% if v.speed > 30 %}
{{v.speed}}
{% endif %}
{% endfor %}
now i just want to count the v.speed values,how can i do that using python or django count function.You can edit my code in that section.
Thank You.
If values is a Django QuerySet you can just use .count()
{% for v in values %}
{% if v.speed > 30 %}
{{v.speed}}
{% endif %}
{% endfor %}
{{values.count}}
You can also use {{values|length}}
Variable assignment is not allowed in django. So, your only alternative is do the counting in python itself & pass the corresponding data to the template.
Thus, you should do the following in python
speedy_values = [v for v in values if v.speed > 30]
And then, pass the speedy_values to your template
{% for v in speedy_values %}
{{v.speed}}
{% endfor %}
{{ v|length }} number of cars have speed greater than 30.
Alternatively, since your QuerySet has been already evaluated, using the length filter would save you an additional query:
{% for v in values %}
{% if v.speed > 30 %}
{{v.speed}}
{% endif %}
{% endfor %}
{{values|length}}
Documentation.
Related
I have some data store in the database In boolean. I want to check if the value is True pick the value and perform some action on it.
product: "{{DB_report_query.product.name}}"
{% if DB_report_query.product.summary == True %}
{{DB_report_query.summary}}
{% endif %}
But this does not work.
If it is a BooleanField then simply use the following:
{% if DB_report_query.product.summary %}
{{DB_report_query.summary}}
{% endif %}
Basically I have what I'm hoping is a simple issue, I just want to check if the Queryset contains more than one object but I'm not sure how to do it? What I've written (that doesn't work) is below.
{% if game.developer.all > 1 %}
<h1>Developers:</h1>
{% else %}
<h1>Developer:</h1>
{% endif %}
Using count() to check the total objects in the QuerySet:
{% if game.developer.all.count > 1 %}
<h1>Developers:</h1>
{% else %}
<h1>Developer:</h1>
{% endif %}
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#pluralize
<h1>Developer{{ game.developer.count|pluralize }}:</h1>
I have this code
{% for time in listOfTimes %}
{% for booking in someOtherList.forloop.parentloop.counter0 %}
{{ booking }}
{% endfor %}
{% endfor %}
The booking variable does not get printed. I think this was because I cannot access someOtherList by using the forloop counter. How would I get the booking value?
Assuming your data is as follows:
listOfTimes = ['time1', 'time2']
someOtherList = [['booking1', 'booking2'], ['booking3', 'booking4']]
Then in template you can do this:
{% for time in listOfTimes %}
{% for booking in someOtherList|get_index:forloop.counter0 %}
{{ booking }}
{% endfor %}
{% endfor %}
Notice the get_index filter in above code, you need to write this custom filter in your app templatetags:
from django import template
register = template.Library()
#register.filter
def get_index(l, i):
return l[i]
Note: Your two list should be of same size, otherwise an IndexError might be raised.
I have a context dictionary entry objectives that maps objective query objects to a list of tests that belong to that objective. Example code:
objectives = Objective.objects.filter(requirement=requirement)
context_dict["requirements"][requirement] = objectives
for objective in objectives:
tests = Test.objects.filter(objective=objective)
context_dict["objectives"][objective] = tests
In my django html template, I iterate over objectives and display them. I then want to iterate over the tests that belong to these objectives. When I do this:
{% for test in {{ objectives|get_item:objective }} %}
I get a TemplateSyntaxError: 'for' statements should use the format 'for x in y':
In the application/templatetags directory, I have:
from django.template.defaulttags import register
...
#register.filter
def get_item(dictionary, key):
return dictionary.get(key)
If instead I make {{ objectives|get_item:objective }} a JS variable, I see that it does indeed produce a list, which I should be able to iterate over. Of course, I can't mix JS variables and the django template tags, so this is only for debugging:
var tests = {{ objectives|get_item:objective }}
var tests = [<Test: AT399_8_1>, <Test: AT399_8_2>, <Test: AT399_8_3>, <Test: AT399_8_4>, <Test: AT399_8_5> '...(remaining elements truncated)...']
How do I iterate over this list in the django template tag?
You cannot user {{...}} inside the {%...%}
What you can try is changing your filter to an assignment tag and using that value in the loop
#register.assignment_tag
def get_item(dictionary, key):
return dictionary.get(key)
And then in your template use it as
{% get_item objectives objective as tests %}
{% for test in test %}
....
{% endfor %}
Instead of all this if your models are proper with foreign keys I would do something like
{% for objective in requirement.objective_set.all %}
{% for test in objective.test_set.all %}
....
{% endfor %}
{% endfor %}
In my context I would pass only the requirement
You already have an answer, but note that dropping the {{ }} tags and keeping everything else the same would have worked fine.
{% for test in objectives|get_item:objective %}
**This is Right Answer for Using Django if else and for loop **
Note :- We Have to Put Key in " " string (Double quotes) some time produce an error so That is good way bcz i faced that problem whwn i Learned
{% if 'numbers'|length > 0 %}
{% for i in numbers %}
{% if i > 20 %}
{{i}}
{% endif %}
{% endfor %}
{% else %}
Empty
{% endif %}
I'm using a list to populate / generate some html code dynamically as shown below,
<ul>
{% if results %}
{% for result in results %}
<li><a href="/accounts/evalpage/" ><strong>{{result.0}}</strong>{{result.1}}</a></li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>
I'm running into an issue with that if one of the list items is clicked, i need to be able to retrieve the information stored in that list item, e.g if item one is clicked i need to retrieve {{result.0}} and {{result.1}} is there a way i can retrieve this information?
List is shown below :
[['User1', 'InProgress'], ['User2'], ['User3'], ['User3'], ['User4']]
For instance, if the row containing User1 and InProgress is clicked by the end user, i want to be able to have the information User1 and InProgress within Django such that operations can be performed with them
It should be clear that in order for the backend to know what object you clicked on, you need to pass that value in the URL. So, you'll need to change the definition of your "evalpage" URL to accept a parameter:
url(r'^accounts/evalpage/(?P<user>\w+)/$', views.evalpage, name='evalpage')
and the view signature:
def evalpage(request, user):
...
and now you can do:
...
try this:
<ul>
{% if results %}
{% for result in results %}
<li>
<a href="/accounts/evalpage/" >
{% for item in result %}
<strong>{{item}}</strong>
{% endfor %}
</a>
</li>
{% endfor %}
{% else %}
<li><strong>No Evaluations to Perform</strong></li>
{% endif %}
</ul>