我需要在哪里传递请求?

当我单击该按钮时,我试图激活一个功能,将数据传输到模板并打开它。这是按钮点击功能:


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.py


url (r '^ create_order /', CreateOrder),

views.py


def 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 ()函数中传递请求或者如何以不同的方式正确地执行它?


MM们
浏览 84回答 1
1回答

噜噜哒

问题出在 urls.py 中url&nbsp;(r&nbsp;'^&nbsp;create_order&nbsp;/',&nbsp;CreateOrder),这将CreateOrder (amount, request):在第一个参数的位置调用所发出的请求(不是任何数字amount)request您需要更改urls.pyamount以在/之后有一个额外的参数from&nbsp;django.urls&nbsp;import&nbsp;path path(r&nbsp;'^&nbsp;create_order&nbsp;/<int:amount>',&nbsp;CreateOrder),&nbsp;#takes&nbsp;a&nbsp;number&nbsp;after&nbsp;the&nbsp;slash然后,这将分别正确地调用作为参数amount和request传递的函数。编辑:第一个参数必须request不是amount,所以你必须将其更改为def&nbsp;CreateOrder&nbsp;(request,amount):
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python