简体   繁体   中英

display two objects by for loop django

my template has a timeline structure.. so 1 object is left aligned and other is right aligned .. i want to display them alternatively within a for loop.. content from same model but alignments of divs are Diffrent..

<ul class="timeline">
                            
                            <li class="timeline-right">
                                <div class="timeline-badge">
                                    <i class="fa fa-calendar"></i>
                                    <p class="date-inverted wow slideInLeft">{{Experience.first.FromDate}} -{{Experience.first.ToDate}}</p>
                                </div><!-- end timeline-badge -->
                                <div class="timeline-panel wow slideInRight">
                                    <div class="experience-content">
                                        <h4>{{Experience.first.Designation}}</h4>
                                        <h5>{{Experience.first.Department}}</h5>
                                        <p>{{Experience.first.ShortNote}}</p>
                                    </div>
                                </div><!--end timeline-panel -->
                            </li><!-- end timeline-right -->
                            <li class="timeline-left">
                                <div class="timeline-badge">
                                    <i class="fa fa-calendar"></i>
                                    <p class="date wow slideInRight">Oct 2015 - Nov 2016</p>
                                </div><!-- end timeline-badge -->
                                <div class="timeline-panel wow slideInLeft">
                                    <div class="experience-content">
                                        <h4>MGS PVT. LTD.</h4>
                                        <h5>Senior Designer </h5>
                                        <p>The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks.</p>
                                    </div>
                                </div><!-- end timeline-panel -->
                            </li><!-- end timeline-left -->

models. py

  class Experience(models.Model):
          Designation = models.CharField(max_length=150)
          Department = models.CharField(max_length=150,default='my dept')
          Organisation =models.CharField(max_length=150)
          ShortNote   = models.CharField(max_length=200)
          FromDate  = models.DateField( default=datetime.date.today)
          ToDate = models.DateField(default=datetime.date.today)

         def __str__(self):
               return self.Designation

You can use forloop.counter with divisibleby filter in the template:

{% for exp in object_list %}
    <li class="{% if forloop.counter|divisibleby:2 %}timeline-right{% else %}timeline-left{% endif %}">
        <div class="timeline-badge">
            <i class="fa fa-calendar"></i>
            <p class="date-inverted wow slideInLeft">{{exp.FromDate}} -{{exp.ToDate}}</p>
        </div><!-- end timeline-badge -->
        <div class="timeline-panel wow slideInRight">
            <div class="experience-content">
                <h4>{{exp.Designation}}</h4>
                <h5>{{exp.Department}}</h5>
                <p>{{exp.ShortNote}}</p>
            </div>
        </div>
    </li>
{% endfor %}

FYI: please follow PEP-8 style guide when naming attributes in a class.

you can use 'cycle' tag inside the for loop.

{% for o in some_list %}
<tr class="{% cycle 'timeline-right' 'timeline-left' %}">
    ...
</tr>

{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM