How to loop a certain number of times in Django?

Posted on 2008-11-18
Last Modified: 2012-05-05
I want loop a certain number of times.. say 5 or 10. I see a lot of code the loop through a collection.
Question by:rajivraj
    LVL 17

    Expert Comment

    for i in range(5):
      code here repeats 5 times

    If that is not what you are looking for then please explain.

    How does this relate to django? To a collection?
    LVL 2

    Expert Comment

    You can use the slice filter in a template like so:

    {% for r in web.results|slice:"0:5" %}
      {{ r.title }}
    {% endfor %}

    LVL 1

    Accepted Solution

    i = 1
    setloop = 5

    for i <= 5:
        <do the loop here>
        i = i + 1

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
    A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now