尽管指定了 post 方法,但 Django 表单已使用 get 方法提交

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

在我呈现的 html 页面中,我有这个表单。单击“下一步”按钮后,表单应作为 post 方法提交,并定向到 url

中的 summary
<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>

在 urls.py 中,summary 被定向到 Summary 视图 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})
]

在摘要视图中 class 我有以下代码

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)

虽然我在表单中指定该方法是 post,但它作为 get 方法提交给了 Summary class。如果我将方法从 def post 更改为 def get,一切正常。

在终端中我收到此错误,表明表单是使用 get 方法提交的。

[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
        

您应该从按钮中删除 onclick 属性

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

onclick 您案例中的处理程序更改当前浏览器 URL 并阻止表单提交。

location.href='/summary' 触发时,浏览器会尝试将 HTTP GET 发送到 /summary 并收到 405,因为您的视图

中没有 def get 处理程序