1

views.py:

def main(request):
    users = PUser.objects.filter(ostan=os, shahr=sh, content_id=content)
    return render(request, 'main.html', {'users': users})

In main.html file:

<a href="#">{{ users.0.website }}</a>
.
.
.
<a href="#">{{ users.1.website }}</a>
.
.
.
<a href="#">{{ users.2.website }}</a>
.
.
.
<a href="#">{{ users.3.website }}</a>

Now I want to put it through a for loop. So I added this line:

numbers = [1, 2, 3, 4]

to views.py, and edited main.html like this:

{% for each in numbers %}
    <a href="#">{{ users.0.website }}</a>
{% enfor %}

But I dont know how to edit {{ users.0.website }} part!

I tried {{ users.each.website }} but it didnt print anything. How should I change it?

niloofar
  • 2,244
  • 5
  • 23
  • 44

1 Answers1

1

What {{ users.each.website }} does is that it searches the key "each" of type string in the dict stored in the variable called users. So it's not the value in a variable called each which is used to look up the value in the dict named users but the raw value "each".

Unfortunately there's no built-in filter in Django's template language to do what you want but you could implement a template filter for that purpose yourself. See answers of this question.

Other than that: Have you remembered to make your variable numbers available in the template?

def main(request):
    // ...
    return render(request, 'main.html', {'users': users, 'numbers': numbers})

... because otherwise the loop will just not be executed at all because numbers inside the template is None. For your special use case you probably could as well just iterate the collection itself:

{% for user in users %}
    <a href="#">{{ user.website }}</a>
{% enfor %}

To only show a limited number of entries you can use the forloop.counter instead of introducing a seperate list of numbers:

{% for user in users %}
    {% if forloop.counter <= 4 %}
        <a href="#">{{ user.website }}</a>
    {% endif %}
{% endfor %}
mxscho
  • 1,990
  • 2
  • 16
  • 27
  • yes I have added `'numbers': numbers` to the code, just forget to mention in my question. It does not work. And there is need to use indexm because I just want to fetch and print the first four items. – niloofar Apr 19 '16 at 11:52
  • I will be able to test it myself in a few hours. If no one else has a working solution until then unfortunately I can just think about what might be wrong. I guess the part without the `for` loop (e.g. `users.0.website`) worked fine? – mxscho Apr 19 '16 at 11:59
  • If you are asking if `{{ users.0.website }}` and `{{ users.1.website }}` and `{{ users.2.website }}` workes fine out of the for loop or not, the answer is yes. – niloofar Apr 19 '16 at 12:02
  • Until I get home and can test it myself - I edited another possibly working solution into my answer at the end. You could try that as well ... it might actually be cleaner than with the additional list of numbers. :) – mxscho Apr 19 '16 at 12:05
  • Your next suggestion worked really thank you. Just there is something more... There are some divs with id like this: `
    ` and `
    `... How can I put them through that for loop? Those numbers should be increased like `10`, `20`, `30` and `40`.
    – niloofar Apr 19 '16 at 13:28
  • 1
    I just got home and tried it myself. I'm sorry that I've overlooked that, but it's actually quiet simple why it doesn't work. I edited my answer above so you can at least mark it as accepted if you like. :) The thing with the `10, 20, ...` is actually another question so I will just cover this here in the comments: Unfortunally you cannot just multiply two numbers with Django's default template filters. If you want to do that you could use the module [django-mathfilters](https://github.com/dbrgn/django-mathfilters) to write something like that: `
    `
    – mxscho Apr 19 '16 at 19:08
  • I faced with error while installing django-mathfilters, so should look for an other solution. But thank you for your first solution that worked. :) – niloofar Apr 20 '16 at 06:09