Jerry Bi
Jerry Bi

Reputation: 341

How do I find the end date from a start date and an interval in Django?

This is related to the question I asked here: My form in Python Django is not submitting

Here is the relevant code

#choices.py

INTERVAL_CHOICES = (('one_day', '1 Day'), ('two_days', '2 Days'), ('three_days', '3 Days'), ('four_days', '4 Days'), ('five_days', '5 Days'), ('six_days', '6 Days'), ('one-week', '1 Week'),
                ('two_weeks', '2 Weeks'), ('three_weeks', '3 Weeks'), ('four_weeks', '4 Weeks'), ('five_weeks', '5 Weeks'), ('six_weeks', '6 Weeks'), ('eight_weeks', '8 Weeks'), ('twelve_weeks', '12 Weeks')
                )

#models.py

class Schedules(models.Model):
    start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today)
    interval = models.CharField(max_length=128, choices=INTERVAL_CHOICES, default='one_day')

#views.py

def schedule_List(request):
    schedule_list = Schedules.objects.all()
    context_dict = {'schedules': schedule_list}
    return render(request, "schedule/schedule_list.html", context_dict)

I need to find an end date from the start date and the interval and then be able to put that end date into the template. I assume I need to somehow convert the second part of the tuple in the interval choices into an integer and then multiply any choices that have 'week' or 'weeks' in them by 7, and then add that to the start date somehow. Does anyone have an idea on this, as well as how to post the end date into the template? Thanks.

EDIT: I should probably mention that I'm using a form in order to put the data into the database. Here is the relevant code for that:

#forms.py
class ScheduleForm(forms.ModelForm):
    start_date = forms.DateField(initial=datetime.date.today, label="Start Date")
    interval = forms.ChoiceField(choices=INTERVAL_CHOICES, initial='one_day', label="Interval")
    class Meta:
        model = Schedules
        fields = ('start_date', 'interval',)

#views.py
def start_One_Schedule(request):
    form = ScheduleForm()
    if request.method == 'POST':
        form = ScheduleForm(request.POST)

    if form.is_valid():
        form.save(commit=True)
        return render(request, 'schedule/schedule.html', {})
    else:
        print(form.errors)

    return render(request, 'schedule/start_one_schedule.html', {'form': form})

Upvotes: 0

Views: 1805

Answers (2)

DragonBobZ
DragonBobZ

Reputation: 2454

It think the best way to do this is store it as a duration as someone suggested, then in the template you can just do

{% for schedule in schedules %}
   <p>End Date: {{ schedule.start_date|add:schedule.interval }}</p>
{% endfor %} 

(disclaimer, I've never tried it). Of course, if you don't want to change your model, you'll have to loop over the objects you queried in the view. Below is what that looks like.

import datetime

def parse_interval(interval):
    interval_dict = {'one_day':{'days':1}, 'two_days':{'days':2}, 'three_days':{'days':3}, 'four_days':{'days':5}, 
    # . . . .
    'twelve_weeks': {'weeks':12}}
    return interval_dict[interval]    

def schedule_list(request):
    schedule_list, new_sched_list = Schedule.objects.all(), []
    for sched in schedule_list:
        sched.end_date = sched.start_date + datetime.timedelta(**parse_interval(sched.interval))
        new_sched_list.append(sched)
    context_dict = {'schedules': new_sched_list}
    return render(request, "schedule/schedule_list.html", context_dict)

And you can just access end_date in the template like you would any other field.

Upvotes: 0

Lev
Lev

Reputation: 1940

Consider using the DuationField. https://docs.djangoproject.com/en/1.11/ref/models/fields/#durationfield

Upvotes: 1

Related Questions