0

I am trying to convert UTC to EST in a view and display this in a template. Currently the code seems to have no effect.

Here is the view:

from django.shortcuts import render, redirect, get_object_or_404
from .models import Article
from .models import Article Image
from .models import Article Specs
from datetime import datetime
from django.utils import timezone
from django.core.paginator import Paginator

def detail(request, article_id):
    article = get_object_or_404(Article, pk=article_id)
    image_all = ArticleImage.objects.all()
    specs_all = ArticleSpecs.objects.all()
    #
    est = pytz.timezone('Pacific/Auckland')
    article_release_datetime = article.releaseDate.astimezone(est)
    #
    datetime_now = datetime.now(timezone.utc)
    datetime_now.astimezone(est)
    release_countdown = article_release_datetime - datetime_now
    #
    (days, remainder) = divmod(release_countdown.seconds, 86400)
    (hours, remainder) = divmod(remainder, 3600)
    (minutes, seconds) = divmod(remainder, 60)
    #

    return render(request, 'articles/detail.html', {'article': article,
                                                 'image_all': image_all,
                                                 'specs_all': specs_all,
                                                 'days': days,
                                                 'hours': hours,
                                                 'datetime_now': datetime_now,
                                                 'minutes': minutes,
                                                 'seconds': seconds,
                                                 'release_countdown': release_countdown})

Here is the template::

<div class="details__countdown-wrapper">
    {% if release_countdown.days <= -1 %}
        <a href="{{ article.get_it_url }}" class="details__get-it-url">Get it</a>
    {% else %}
        <span class="detail_countdown">⏱️</span>
        <span class="details__dropping">DROPPING IN:</span>
        {% if days %}
            {{ days }}d
        {% endif %}
        {% if hours %}
            {{ hours }}hr
        {% endif %}
        {% if minutes %}
            {{ minutes }}m
        {% endif %}
        {% if seconds %}
            {{ seconds }}s
        {% endif %}
        (EST)
    {% endif %}
</div>
Kishan Mehta
  • 2,598
  • 5
  • 39
  • 61
  • Welcome to SO. Whenever you paste some code please use command `CTRL+K` command to format it properly. – Kishan Mehta Jan 02 '21 at 04:27
  • The first thing I notice is that you're not doing anything with the return value of: `datetime_now.astimezone(est)`. – Kevin Christopher Henry Jan 02 '21 at 14:16
  • @KevinChristopherHenry forgive my limited understanding of python. Is datetime_now.astimezone(est) not being passed back into datetime_now = datetime.now(timezone.utc)? Maybe I need to create a variable to use datetime_now.astimezone(est)? – Christopher Jan 03 '21 at 02:36
  • Make sure to read the documentation for the objects and functions you're using. The [datetime documentation](https://docs.python.org/3/library/datetime.html) points out that its objects are immutable, and that the [astimezone()](https://docs.python.org/3/library/datetime.html#datetime.datetime.astimezone) function will "return a `datetime` object". So, yes, you need to assign that return value to something if you want to reference it again later. – Kevin Christopher Henry Jan 03 '21 at 16:13
  • @KevinChristopherHenry thank you for taking the time to review. That points be on the right track! – Christopher Jan 03 '21 at 23:53

0 Answers0