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

  • Home
  • SEARCH
  • 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 8700845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:15:45+00:00 2026-06-13T02:15:45+00:00

In my django template I have some Event items listed using a for loop.The

  • 0

In my django template I have some Event items listed using a for loop.The event has an associated date_of_occurrence attribute which is a python DateTime.I need to make the background for the event red ,if the datetime is less than current datetime.

I thought of doing it like this

In template

<ul>
{% for  event in events %}
  <div id="event_div"
    <li>
       event.name<br>
       event.date_of_occurrence
    </li>
  </div>
{% endfor %}
</ul>

In css

.pastevent{
  background-color:red;
}

I need a javascript function to set the class of element with id event_div to pastevent.Can someone tell me how this function can be implemented?

As I understand the function is not fired by any click event ,but when page loads. How will the datetime value of event passed from the template to do the comparison to the current javascript date?

  • 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-06-13T02:15:47+00:00Added an answer on June 13, 2026 at 2:15 am

    You wouldn’t necessarily have to use javascript to do this. There are a couple of different options using the template tags.

    The simpler approach (and more crude) would be to render the current datetime to the template as eg. current_date then do a logic check in your template.

    {% for  event in events %}
        {% if event.date_of_occurrence < current_date %}  
        <div id="pastevent">
        {% else %}
        <div id="event_div">
        {% endif %}
        <li>
           event.name<br>
           event.date_of_occurrence
        </li>
      </div>
    {% endfor %}
    

    The other option would be to create a custom template filter that returns whether the event was in the past or not and apply the same logic as above.

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

Sidebar

Related Questions

I have a django template which is used from many views. The template has
I have some french letters (é, è, à...) in a django template but when
I have the following code in a template, using a javascript event calendar: <script
I try Netbeans 6.7 for python but don't have a good django template highlight
I have some problem to figure out how new django views (template view) and
I am using the standard User model (django.contrib.auth) which comes with Django. I have
I have a page which uses jQuery for some AJAX-stuff und Django as main
I have some problems with making Javascript work with Django template inheritance. According to
i have a django template - one that is normally loaded via a standard
I have a Django template that I want to extend in multiple places. In

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.