This is my first post so please be gentle a newbie at coding and Django,
I have created a view that when a job is created there is an option for the job to reoccur every 4 or 6 weeks, the problem is when I create the job the 2nd occurence is 8 week from the intial date and then subsequent dates are in 4 week period like below, hope this makes sense
Jan. 1, 2023 Every 4 weeks
Feb. 26, 2023 Every 4 weeks
March 26, 2023 Every 4 weeks
April 23, 2023 Every 4 weeks
here is my views.py
**def create_job(request):**
** form = JobForm(request.POST or None)**
** if request.user.is_authenticated:**
** if request.method == "POST":**
** if form.is_valid():**
** job = form.save() # Save the job object**
** # Calculate the invoice amount**
** amount = job.cost**
** # Calculate the next occurrence date based on the selected occurrence frequency**
** if job.occurrence == 'Every 4 weeks':**
** next_date = job.date + timedelta(weeks=4)**
** elif job.occurrence == 'Every 6 weeks':**
** next_date = job.date + timedelta(weeks=6)**
** else:**
** next_date = job.date # Default to the job date if no valid occurrence is selected**
** # Check if an invoice already exists for the next occurrence date**
** existing_invoice = Invoice.objects.filter(client=job.client, date=next_date).exists()**
** if not existing_invoice:**
** # Create the invoice with the next occurrence date**
** invoice = Invoice.objects.create(client=job.client, date=next_date, amount=amount)**
** messages.success(request, "Job created successfully")**
** # Create occurrences for the next 5 years**
** if job.occurrence == 'Every 4 weeks':**
** for i in range(1, 195): # Create 195 occurrences (5 years)**
** next_date = job.date + timedelta(weeks=4 * i)**
** existing_invoice = Invoice.objects.filter(client=job.client, date=next_date).exists()**
** if not existing_invoice:**
** Job.objects.create(job_type=job.job_type, client=job.client, date=next_date, occurrence=job.occurrence, cost=job.cost)**
** elif job.occurrence == 'Every 6 weeks':**
** for i in range(1, 130): # Create 130 occurrences (15 years)**
** next_date = job.date + timedelta(weeks=6 * i)**
** existing_invoice = Invoice.objects.filter(client=job.client, date=next_date).exists()**
** if not existing_invoice:**
** Job.objects.create(job_type=job.job_type, client=job.client, date=next_date, occurrence=job.occurrence, cost=job.cost)**
** return redirect('job_list')**
** else:**
** messages.error(request, "Error creating job")**
** print(form.errors)**
** **
** else:**
** return redirect('home')**
** return render(request, 'create_job.html', {'form': form})**
**```**
thanks for any guidance