Finding 2 values if available in a list in a Django Template

A_K

I have the following list called `` :[(2, 'Flat Bench'), (1, 'Flat Bench'), (3, 'Flat Bench')]

I am trying to find if the b.order which is the first number and e.name which is the 2nd string is available from a django template. In my trial I made sure that b.order is equal to 2 and e.name is 'Flat Bench'.

Here is the django template:

  {% if b.order and e.name in all_logs %}
  available
  {% else%}
  not available
  {% endif %}

In my case there is no error, it is just always showing not available even if it is available.

I am not sure what change I should be doing to get this code to work.

Abdullah

Well, I would suggest to create custom tags as explained here: https://docs.djangoproject.com/en/4.1/howto/custom-template-tags/

Your custom function should look as follows:

my_custom_tags.py

from django import template

register = template.Library()


@register.simple_tag
def tuple_check(items, key, value):
    return (key, value) in items

Your template should have the following:

{% load my_custom_tags %}
...
some codes here
...

{% tuple_check your_list b.order e.name as item_check_result %}

{% if item_check_result %}
Available
{% else %}
Not Available
{% endif %}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django - Show list datetime values in template

Django template: How to use values/values_list

Django Template: How to display the value of Foreign Key in a values_list

Finding two smallest values in a list

Obtaining top 2 max values from a list and finding its corresponding value from 2nd list

Comparing values in django template

unique values in django template

Finding values in a matrix from list of values in R

Django template : Need 2 values to unpack in for loop; got 8

display list in django template

Django Template extract list

Request object not available in template tags in Django 1.11

Django: Include template with constructed name if available

how to list available arguments in a jinja template

Django: template to list data from 2 models related each other

Filter 2D list with template tag in Django

Finding list of versions available in a Maven repository for a specific plugin?

Finding the most common Values in a list of ndarrays in Python

Finding a list of keys containing equal values in a dictionary

Excel Help for Finding List of Maximum Values

Finding a sublist in a list and sum its values

Finding values greater than * in a map list

Finding Min and Max Values in List Pattern in Python

Finding values in elements of a list greater than X

Finding nested list with identical values in Dictionaries [Python]

Slope Intercept Formula for Finding list of y values

Optimising Finding Closest Value in a List of Values

PYTHON: Finding the average of values of a nested list

Finding items in a list and then displaying back those values