简体   繁体   中英

Django filter field on the context processor

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM