Django form got submitted with the get method although post method was specified

In my rendered html page I have this form. Upon clicking on the Next button, the forms should be submit as a post method, being directed to the summary in url

<form action="/summary" method="post">
            <input type="hidden" name="csrfmiddlewaretoken" value="iC3L3QCDundSemg1jfZH96w8X83jrsaE3gQmtbb3rFCyNEN9jXdubao0TJ18EKnb">    
            <label for='customerName' class="subTitle">Name</label>
            <br>
            <input type="text" name="customerName" maxlength="100" required id="id_customerName">
            <br>
            
            <br>
            <label for="email" class="subTitle">Email</label>
            <br>
            <input type="email" name="email" required id="id_email">
            <br>
            <br>

            <label for="phone" class="subTitle">Mobile Phone</label>
            <br>
            <input type="tel" name="phone" required id="id_phone">
            <br>
            <br>

            <label for="comment" class="subTitle">Comment</label>
            <br>
            <textarea name="comment" cols="20" rows="10" maxlength="500" required id="id_comment">
</textarea>
            <br>
            <button onclick="location.href='/deliveryFormCheckout'" type="button" class="btn btn-danger btn-lg">Back</button>
            <button onclick="location.href='/summary'" type="submit" class="btn btn-success btn-lg" id="deliveryNextBtn">Next</button>
        </form>

In the urls.py, the summary is then directed to the Summary view class

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', indexPage.as_view()),
    path('dimsumshop', MainShop.as_view(), name = 'mainShop'),
    path('box/<slug:productSlug>', ProductView.as_view(), name = 'viewProduct'),
    path('product/<slug:boxSlug>', BoxView.as_view(), name = 'BoxView'),
    path('changeItemQuantityInBasket', ChangeItemQuantity.as_view()),
    path('isOrderPriceAboveMinimum', MinimumOrderPriceFulfilled.as_view()),
    path('checkout', Checkout.as_view(), name = 'checkout'),    
    path('deliveryFormCheckout', DeliveryFormView.as_view(), name = 'deliveryFormView'),
    path('checkSteamer', CheckoutCheckSteamer.as_view()),
    path('verifyAddress', VerifyAddress.as_view(), name = 'verifyAddress'),
    path('checkoutCustomer', CustomerInfo.as_view(), name = 'customerInfo'),
    path('summary', Summary.as_view(), name = 'summary'),
    re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT})
]

In the Summary view class I have the below code

class Summary(View):
    def post(self, request, *args, **kwargs):
        print('post called')
        form = CustomerCheckoutInfoForm(request.POST)
        context = dict()
        return render(request=request, template_name='summary.html', context = context)

Although I specificed in the form that the method is a post, it got submitted as a get method to the Summary class. If I change the method from def post to def get, everything worked fine.

In the terminal I receive this error showing that the form was submitted using the get method.

[15/Jan/2022 17:24:01] "GET /favicon.ico HTTP/1.1" 404 3785
Method Not Allowed (GET): /summary
Method Not Allowed: /summary
[15/Jan/2022 17:24:05] "GET /summary HTTP/1.1" 405 0
        

Solution 1:

You should remove onclick attribute from your button

<button type="submit" class="btn btn-success btn-lg" id="deliveryNextBtn">Next</button>

onclick handler in your case changes current browser URL and prevents form from submitting.

When location.href='/summary' fires, browser tries to send HTTP GET to /summary and receives 405, because you have no def get handler in your View