0
votes

I am getting a ValueError that the class below didn't return any httpresponse when i try to redirect to a template. the redirect is supposed to go to the stripe payment view.

here is an entire class that has the redirect call

class CheckoutView(View):
def get(self, *args, **kwargs):
    form = forms.CheckoutForm()
    context = {
        'form': form
    }

    return render(self.request, "checkout.html", context)

def post(self, *args, **kwargs):
    form = forms.CheckoutForm(self.request.POST or None)
    try:
        equipment_order = models.EquipmentOrder.objects.get(user=self.request.user, ordered=False)
        if form.is_valid():
            street_address = form.cleaned_data.get('street_address')
            apartment_address = form.cleaned_data.get('apartment_address')
            country = form.cleaned_data.get('country')
            zip = form.cleaned_data.get('zip')
            '''
            TODO: add functionality to these fields
            same_shipping_address = form.cleaned_data.get('same_shipping_address')
            save_info = form.cleaned_data.get('save_info')
            '''
            payment_option = form.cleaned_data.get('payment_option')
            billing_address = models.BillingAddress(
                user=self.request.user,
                street_address=street_address,
                apartment_address=apartment_address,
                country=country,
                zip=zip
            )
            billing_address.save()
            equipment_order.billing_address = billing_address
            equipment_order.save()

            if payment_option == 'S':                
                return redirect('create:payment', payment_option='stripe')
            elif payment_option == 'P':
                return redirect('create:payment', payment_option='paypal')
            else:
                messages.warning(self.request, "Invalid payment option")
                return redirect('create:checkout')
    except ObjectDoesNotExist:
        messages.error(self.request, "You do not have an active order")
        return redirect("create:order_summary")
2

2 Answers

0
votes

1) Remove the try/except i think its better

2) I think you have a problem on 'payement_option' , maybe it doesnt give any value of expected , try to print it first to see what does it give

3) remove the ' or None ' from CheckoutForm

4) you can avoid using 'self' by importing form in that way :

from .forms import CheckoutForm
...
form = CheckoutForm(request.POST)
0
votes

The above answer may work fine but as I tried your code it throws the same error as you described whenever you leave the form field empty or no payment method is selected.

After trying your code the best possible solution I figure out is this. I know this is not a perfect solution but it worked 😅

Suggestion: Try to move your else statement under if instead of nesting it after elif statement. And change your else to given below.

Old:

    else:
       messages.warning(self.request, "Invalid payment option select")
       return redirect('core:checkout')

New:

    else :
        messages = 'Invalid payment option select'
        return HttpResponse(messages)

Proof: Invalid payment option select