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 6860363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:23:58+00:00 2026-05-27T02:23:58+00:00

I need to output objects in Django template, so that each object has its

  • 0

I need to output objects in Django template, so that each object has its own template. Templates are keeped in the variable “templates” like that – [‘path/to/template1′,’path/to/template2’, …]

Is there a way to “cycle” these templates in object loop, somehow like that:

{% for object in objects %}
   {% cycle templates as template %}
   {% include template %} // this code is just for example
{% endfor %}

I cannot include these templates directly into objects list, because it is generated by paginator’s template tag.

Any ideas? Thanks.

  • 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-27T02:23:59+00:00Added an answer on May 27, 2026 at 2:23 am

    I’ve written the template tag that receives a list variable and cycles it through the loop.

    from django import template
    from django.template.base import TemplateSyntaxError, Node
    
    from itertools import cycle as itertools_cycle
    
    register = template.Library()
    
    class CycleListNode(Node):
        def __init__(self, list_variable, template_variable):
            self.list_variable = list_variable
            self.template_variable = template_variable
    
        def render(self, context):
            if self not in context.render_context:
                # First time the node is rendered in template
                context.render_context[self] = itertools_cycle(context[self.list_variable])
            cycle_iter = context.render_context[self]
            value = cycle_iter.next()
            if self.template_variable:
                context[self.template_variable] = value
            return ''
    
    @register.tag
    def cycle_list(parser, token):
        args = token.split_contents()
        if len(args) != 4 or args[-2] != 'as':
            raise TemplateSyntaxError(u"Cycle_list tag should be in the format {% cycle_list list as variable %}")
        return CycleListNode(args[1], args[3])
    

    It’s pretty simple but solves the issue.

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

Sidebar

Related Questions

I'm writing an XSLT template that need to output a valid xml file for
I have a list of objects which need to be output 2 items per
I need to loop through my django list that I have passed to the
I have an object (Ticket), which has a list of other objects (Message). Message
I've got a list of objects that I need to organize as an aesthetic
I have a collection on objects that I need to assign a random order.
I am outputting a series of Django objects in a template: {% for obj
I need to randomly scatter a variable number of objects in a Flash movie.
My problem is that I need for a graphical output of an algorithm a
I often have the need to search a javascript array that contains objects. I

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.