Use for loop variable as a dictionary key in a Django template

I am trying to do the following.

The counter loop must go through a loop for all values, and there cannot be a user associated with each quantity, but the count value imust be used in each loop to jump to JavaScript.

part of python:

users = {}
users[1]={}
users[1][id]=...
users[1][email]=...

...

count=[1,2,3,4,5,6,7,8,9,10]

Part of the Django template:

{% for i in count %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1

email:{% if users.i.email %}'{{users.i.email}}'{% else %}null{% endif%}
{% endfor %}

This does not return anything for email. When I substitute a number 1for iin {% if user.i.email %}, email returns the email address of users. I use data in JavaScript, so it must be implicitly null if it does not exist. I cannot get Django to recognize a variable ias a variable instead of the value of i.

[] ,

email:{% if users.[i].email %}'{{users.[i].email}}'{% else %}null{% endif%}

<with

{% for i in count %}{% with current_user=users.i %}...

current_user.email,

{% for i in count %}{% with j=i.value %}...

, , j, .

inner for, , /, .

, Django i ?

Jayd

* Edit:

, , .

{% for i in count %}
  {% for key, current_user in users.items %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
  {% endfor %}
{% endfor %}

, do some stuff with the value i . if if:

{% for i in count %}
  {% for key, current_user in users.items %}
    {% if i == key %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
    {% endif%}
  {% endfor %}
{% endfor %}

, .

, current_user.

{% for i in count %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:{% for key, current_user in users.items %}{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}{% endfor %}
{% endfor %}

. ?

, , , , i :

{% with current_user=users|getuser:i %}

, , , i "i" .

.

* Edit

. {{}}, , {% %} .

+5
4

:

:

@register.filter(name='dict_value_or_null')
def dict_value_or_null(dict, key):
    if key in dict:
        return dict[key]
    else:
        return 'null'

{% for i in count %}
      do some stuff with the value i using {{i}} which always returns the value, i.e. 1
      email:'{{users|dict_value_or_null:i|dict_value_or_null:'email'}}'
{% endfor %}

, , . , , {%%} , .

?

+1

, . .

users = []
user = {'id':..., 'email': ...}
users.append(user)

...

{% for user in users %}
    {{ user.email }}
{% endfor %}

, {{ forloop.counter }}.

+9

:

{% for key,val in user.items %}
ID is {{ val.id }} and Email is {{ val.email }}
{% endfor %}
+1

Jayd dict,

@register.filter(name='dict_value_or_null')
def dict_value_or_null(dict, key):
    try:
        return dict[key]
    except:
        return 'null'
+1

All Articles