0

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

1 Answers1

1

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 %}
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
  • Rigth, but want i want to do is return the field that has id 2 –  Aug 12 '14 at 20:41
  • I changed my answer above. It would just be `model.field == 2` – Aaron Lelevier Aug 12 '14 at 22:15
  • It doesnt work :/ , what i want to do is load all the users and count the ones who have some caracteristics , for example sex male or female , i cant do that with your code –  Aug 12 '14 at 23:32
  • Right and in that "#do something" i want to show the user name –  Aug 13 '14 at 01:15