I’m encountering an issue with one of my Django views, and I’m hoping someone can help me troubleshoot it.
When I try to access the URL: https://tout-talent.com/forfait/, the browser or site displays a different page with the message:
{"success": false, "message": "Invalid request method"}
However, on my server and in the debug.log file, I see the following log entry:
"GET /forfait/ HTTP/1.1" 200
indicating a 200 OK HTTP status code. But the template that should normally display is not being rendered.
Here’s the code for the view :
@login_required
def subscribe(request):
if not hasattr(request.user, 'company'):
print("Utilisateur non connecté ou sans compagnie associée.")
return redirect('login') # Or show an appropriate message
company = request.user.company
print(f"Company: {company}")
active_subscription = None
try:
active_subscription = CompanySubscription.objects.get(company=company, is_active=True)
print(f"Active subscription found: {active_subscription}")
except CompanySubscription.DoesNotExist:
print("No active subscription found.")
if request.method == 'POST':
form = SubscriptionForm(request.POST)
if form.is_valid():
plan = form.cleaned_data['plan']
print(f"Selected plan: {plan}")
if plan.name == 'lite':
print("Redirecting to Lite plan duration view.")
return redirect('days_duration_for_lite_plan', plan_ref=plan.ref)
else:
days = plan.duration_days
amount = plan.price_per_month if days >= 30 else plan.price_per_day * days
print(f"Days: {days}, Amount: {amount}")
if plan.name == 'free':
subscription, created = CompanySubscription.objects.get_or_create(
company=company,
defaults={
'plan': plan,
'start_date': timezone.now(),
'end_date': timezone.now() + timedelta(days=days),
'is_active': True,
}
)
if created:
print("New free subscription created.")
else:
subscription.plan = plan
subscription.end_date = subscription.calculate_end_date()
subscription.is_active = True
subscription.save()
print("Free subscription updated.")
return redirect('mon_forfait') # Redirect to the user's subscription page
# If not free, redirect to the payment page
amount = int(amount) # Convert amount to integer
print(f"Redirecting to payment page with amount: {amount}")
return redirect('payment', company_ref=company.ref, plan_ref=plan.ref, days=days, amount=amount)
else:
print("Form is invalid. Errors: ", form.errors)
else:
print("GET request received.")
form = SubscriptionForm()
# Retrieve subscription plans
free_plan = SubscriptionPlan.objects.get(name='free')
lite_plan = SubscriptionPlan.objects.get(name='lite')
pro_plan = SubscriptionPlan.objects.get(name='pro')
context = {
'form': form,
'active_subscription': active_subscription,
'free_plan': free_plan,
'lite_plan': lite_plan,
'pro_plan': pro_plan,
}
return render(request, 'subscribe.html', context)
I’m encountering an issue with one of my Django views, and I’m hoping someone can help me troubleshoot it.
Samuel.amou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1