當我單擊該按鈕時,我試圖激活一個功能,將數據傳輸到模板并打開它。這是按鈕點擊功能:function openForm () { const amount = select.value; $ .ajax ({ url: '/ create_order /', type: 'get', data: {'amount': amount}, dataType: "json", xhrFields: {withCredentials: true}, success: function (data) { if (data ['content']) { $ ('body'). append (data ['content']); console.log (data ['content']) } }, });}urls.pyurl (r '^ create_order /', CreateOrder),views.pydef CreateOrder (amount, request): count = amount.GET.dict () content = mark_safe (render_to_string ( 'form.html', { 'amount': count ['amount'], }, request))問題出在請求參數上。我在最后一行需要它,所以我需要將它傳遞給 CreateOrder 函數。但這就是錯誤發生的方式TypeError at / create_order / ?CreateOrder () missing 1 required positional argument: 'request'告訴我如何在openForm ()函數中傳遞請求或者如何以不同的方式正確地執行它?
1 回答

嚕嚕噠
TA貢獻1784條經驗 獲得超7個贊
問題出在 urls.py 中
url (r '^ create_order /', CreateOrder),
這將CreateOrder (amount, request):
在第一個參數的位置調用所發出的請求(不是任何數字amount
)request
您需要更改urls.pyamount
以在/之后有一個額外的參數
from django.urls import path path(r '^ create_order /<int:amount>', CreateOrder), #takes a number after the slash
然后,這將分別正確地調用作為參數amount
和request
傳遞的函數。
編輯:第一個參數必須request
不是amount
,所以你必須將其更改為
def CreateOrder (request,amount):
添加回答
舉報
0/150
提交
取消