当我单击该按钮时,我试图激活一个功能,将数据传输到模板并打开它。这是按钮点击功能: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 回答
![?](http://img1.sycdn.imooc.com/5458655200013d9802200220-100-100.jpg)
噜噜哒
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
提交
取消