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 googled but unable to find the solution. Where I went thought some answers like This answer
Some official docs which I went through Trans Tag Built-In Tags
Template View
{% load custom_tags %}
{% for each in content %}
{% with tempID='' %} # It is initially empty string
{% if each.contentID != tempID %}
{% tempID = each.contentID %} # Want to assign new value to tempID
....
..
{% endif %}
{% endwith %}
{% endfor %}
Is there any method to assign with tag variable. I tried custom_tags filters also. Not worked.
Is there any method to assign with tag variable?
Yes, you can simply use the {% with tempID= each.contentID %} which in this case will assign a new value to the variable.
Alternative
Variables in templates are merely used to display values and are not often used in a way in which calculation or reassignment is performed. In such cases though you can solve it by using a Django custom filter. See the example below.
.py file - Assign a value to a variable
#register.filter(name='update_variable')
def update_variable(value):
data = value
return data
.HTML file
{% with "true" as data %}
{% if data == "true" %}
//do somethings
{{update_variable|value_that_you_want}}
{% else %}
//do somethings
{% endif %}
{% endwith %}
Note that when you are using Django filters the variable instance resides in Python and not directly in the Template.
I want to filter a result of a field in django in an html file.
Something like this
{{ model.field where id = 2 }}
I've been looking for in django docs but i only could find a way to do it on the views.py.
I also so something like javascript when u write a "|" simbol after the request but i still couldnt archieve it
You can use the {% if %} template tag. So:
{% if model.field == 2 %}
# do something
{% endif %}
Here is the official documentation:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#operator
Edit:
If model.field has a value of 2 then it just needs to be the above.
Edit 2:
Without seeing your code, it is hard to tell, but here is how to filter for Users based on Gender in a template:
{% for user in users %}
{% if user.gender == "male" %}
# do something
user.username
{% endif %}
{% endfor %}
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.
I want to see if a field/variable is none within a Django template. What is the correct syntax for that?
This is what I currently have:
{% if profile.user.first_name is null %}
<p> -- </p>
{% elif %}
{{ profile.user.first_name }} {{ profile.user.last_name }}
{% endif%}
In the example above, what would I use to replace "null"?
None, False and True all are available within template tags and filters. None, False, the empty string ('', "", """""") and empty lists/tuples all evaluate to False when evaluated by if, so you can easily do
{% if profile.user.first_name == None %}
{% if not profile.user.first_name %}
A hint: #fabiocerqueira is right, leave logic to models, limit templates to be the only presentation layer and calculate stuff like that in you model. An example:
# someapp/models.py
class UserProfile(models.Model):
user = models.OneToOneField('auth.User')
# other fields
def get_full_name(self):
if not self.user.first_name:
return
return ' '.join([self.user.first_name, self.user.last_name])
# template
{{ user.get_profile.get_full_name }}
You can also use another built-in template default_if_none
{{ profile.user.first_name|default_if_none:"--" }}
You can also use the built-in template filter default:
If value evaluates to False (e.g. None, an empty string, 0, False); the default "--" is displayed.
{{ profile.user.first_name|default:"--" }}
Documentation:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#default
isoperator : New in Django 1.10
{% if somevar is None %}
This appears if somevar is None, or if somevar is not found in the context.
{% endif %}
Look at the yesno helper
Eg:
{{ myValue|yesno:"itwasTrue,itWasFalse,itWasNone" }}
{% if profile.user.first_name %} works (assuming you also don't want to accept '').
if in Python in general treats None, False, '', [], {}, ... all as false.
In cases where we need to validate for a field with null value, we may check so and process as under:
{% for field in form.visible_fields %}
{% if field.name == 'some_field' %}
{% if field.value is Null %}
{{ 'No data found' }}
{% else %}
{{ field }}
{% endif %}
{% endif %}
{% endfor %}
You could try this:
{% if not profile.user.first_name.value %}
<p> -- </p>
{% else %}
{{ profile.user.first_name }} {{ profile.user.last_name }}
{% endif %}
This way, you're essentially checking to see if the form field first_name has any value associated with it. See {{ field.value }} in Looping over the form's fields in Django Documentation.
I'm using Django 3.0.
Just a note about previous answers: Everything is correct if we want to display a
string, but pay attention if you want to display numbers.
In particular when you have a 0 value bool(0) evaluates to False and so it will not display and probably is not what you want.
In this case better use
{% if profile.user.credit != None %}