Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:55:16+00:00 2026-05-26T01:55:16+00:00

I am trying to get the following working. The count loop needs to loop

  • 0

I am trying to get the following working.

The count loop needs to loop through for all values, and there may not be a user associated with each count, but the count value i needs to be used in each loop to pass to the JavaScript.

python part:

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

...

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

Django template part:

{% 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 returns nothing for email.
When I substitute the number 1 for i in {% if user.i.email %} email returns the users email address.
I’m using the data in JavaScript, so it needs to be implicitly null if it doesn’t exist.
I can’t seem to get Django to recognize the i variable as a variable instead of the value i.

using [] doesn’t work, as it throws an invalid syntax error

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

I have tried using “with” statement

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

and then using current_user.email, but returned nothing

Have also tried

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

just in case it would work, and then trying to use j, but same result.

I have thought about creating an inner for loop that loops over the user object and check if i is equal to the key/value, but that seems expensive and not very scalable.

Any ideas how I can force Django to view i as a variable and use it’s value as an index, or any other way get around this?

Thanks

Jayd

*Edit:

I tried the extra for loop, as suggested by Abhi, below.

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

This sort of works, but now it will repeat do some stuff with the value i for every value of users. and if I put in an 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 %}

That ignores the loops when a count doesn’t have a particular user.

The only way I can see around this is the have the user loop at each place that I want to use 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 %}

And this seems very expensive to do.
Any ideas?

I was thinking of maybe writing a filter that returns the values for users using i as the key:

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

But I don’t know if this will work or I will get the same error, where i is being passed as the value ‘i’ instead of a variable name.

I will give it a try so long.

*Edit

This didn’t work.
The filter worked using {{}} returning the object, but it didn’t work inside the {% %}
.

Thanks for the input

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:55:17+00:00Added an answer on May 26, 2026 at 1:55 am

    I have come up with the following as a work around:

    This is the filter code:

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

    Here is the template code

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

    This works well, so I suppose that have my solution. But I still think this all could have been much easier if there was a way in the template system to force values inside {% %} to be treated as variables, instead of literals.

    Is there a way to do this?

    Thanks for the input

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
I am trying to get the following loop working to fill an array of
Im trying to get debugging working without an app.config. I have the following code:
I am trying to get a dojo comboBox working in Zend, with the following
I'm trying to get this sample for AJAX to WCF working, with the following
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I'm trying to get friends count via my account using Facebook API. Its working
Following is my javascript program. I am trying to get all child tags of
I' trying to get the following string to output to a cell in Excel
I'm trying to get the following bit of code to work in LINQPad but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.