adjusting period of time for upcoming birthdays with django-birthday

Hi there,

I#m trying to adjust the period of Time for upcoming birthdays according to the documentation of “django-birthday” from today to tomorrow, but i am new to Django and Python, maybe i make a syntax-failure?

My code:
Hi, want to change the period for upcoming BDays being counted from tomorrow, not today. i am new to python and django, so i am sure it is my fault and i forgot something in the syntax:

def my_view(request)

return render (request, bday.html {
'upcoming_birthdays': Employees.objects.get_upcoming_birthdays(after=1)
})

delivers a “‘str’ object has no attribute ‘timetuple’” error in runtime…

if i leave () blank in the end of the code, everything works fine aside the fact that today is also counted for upcoming birthdays…

django-birthday doc:
https://django-birthday.readthedocs.io/en/latest/index.html

Thx in advance for any tips

Hey there!
It will be helpful to include the template you’re rendering, and the traceback that’s displayed on the console.

Template:

{% extends 'base.html' %}

{% block mycss %}{% include "#tabelFormatting.html" %}{% endblock %}

{% block content %}
{% include "#searchBadge.html" %}
<div class="container">

 <div class="table-responsive mx-auto w-75" style="position: relative; height: 20rem">
    <table class="table table-hover table-striped">
      <thead class="bg-primary-subtle border-primary text-primary">
        <tr>
            <th scope="col" class="text-center">#</th>
            <th scope="col">DG/Anrede</th>
            <th scope="col"></th> <!--Spalte für akademische Titel, ohne Überschrift-->
            <th scope="col">Nachname</th>
            <th scope="col">Vorname</th>
            <th scope="col" class="text-center">Alter</th>
            <th scope="col" class="text-end">
                <nobr><i class="fas fa-gift"></i>&nbsp;<i>Geburtstag</i>&nbsp;<i class="fas fa-gift"></i>&nbsp;</nobr>
            </th>
        </tr>
      </thead>

      <tbody>

        {% for birthday in birthdays %}
          {%  if birthday.birthday != None %} <!--leere Felder nicht anzeigen-->

            <tr>
                <th class="text-center" scope="row">{{ forloop.counter }}</th>
                <td><nobr>{{ birthday.military_rank }}</nobr></td>
                <td class="text-end"><nobr>{{ birthday.prefix_title|default_if_none:"" }}</nobr></td>
                <td><nobr><b>{{ birthday.last_name }}</b></nobr></td>
                <td><nobr>{{ birthday.first_name }}</nobr></td>
                <td class="text-center"><nobr>('{{ birthday.age }})</nobr></td>
                <td class="text-end"><nobr><b>{{ birthday.birthday }}</b></nobr></td>
            </tr>
          {%  endif %}
        {%  endfor %}

      </tbody>
    </table>
</div>
<div class="mb-5"></div>

{% endblock %}

can send yout the tracxeback as pdf by PN

The traceback can be posted here, the same as you do with code.

It’s hard to be precise without it, but i think that this is incorrect, you have a upcoming_birthdays key defined on the context.

But you’re iterating over a birthdays key.

no, that’s correct (and working)…should have posted the view also i think:

@login_required
def birthday(request):

    context= {
        'today_birthdays': Employees.objects.get_birthdays(),
        'upcoming_birthdays': Employees.objects.get_upcoming_birthdays(days=8),
        'birthdays': Employees.objects.order_by_birthday()
        }

    return render(request, 'birthday.html', context)

The problem ist to tell the “.get_upcoming_birthdays(days=8),” when to start counting, see also linked documentation in my first post. Restriction to 8 days as an argument works, but trying the “after=1” argument results in “missing timetuple” error.

I see.
I’ve read the documentation, and it says something really interesting

Warning : This library is pretty hacky. Use it if you’re lazy, not if you’re concerned about your code quality!

I think you may be better implementing this on your own, the library seems to be without any work from 2 years from now.

yeah, i was somehow afraid of this kind of judgement, i also sended an email zto the owner of the github repository with no answer yet…too bad.
There are also some faults in dealing with birthdays in leap years…so i think i will have to do it myself in the end but thanks for your thoughts :slight_smile:

1 Like

So in case, somebody should have similar problems: THIS Solution is working for my purposes:

@login_required
def birthday(request):
    # For TODAY Birthdays
    datetime_now = timezone.now()
    now_day, now_month = datetime_now.day, datetime_now.month

    # For Birthdays within the next 7 days
    def day_tuple(day_offset):
        today = timezone.now().date()
        date = today + timedelta(day_offset)
        return (date.day, date.month)

    one_week = timedelta(weeks=1)
    seven_days = [day_tuple(day) for day in range(one_week.days)]

    upcoming_birthdays = []
    for day, month in seven_days:
        upcoming_birthdays += Employees.objects.filter(
            birthday__day=day,
            birthday__month=month
        )

    context= {
        'today_birthdays': Employees.objects.filter(birthday__day=now_day, birthday__month=now_month),
        'upcoming_birthdays': upcoming_birthdays,
        'all_birthdays': Employees.objects.exclude(birthday__isnull=True)
        }
    return render(request, 'birthday.html', context)