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?

1 Answer 1

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 %}
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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?
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.
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. :)
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 to write something like that: <div id="gallery{{ forloop.counter|mul:10 }}">
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.