Django:将阵列从模板发送到视图

时间:2018-08-01 06:09:16

标签: django python-3.x django-rest-framework django-templates django-views

脚本

 function submition(){
        var result=finalaproduct;
        alert(result)
        $.ajax({
            headers: { "X-CSRFToken": '{{csrf_token}}' },
            type: "POST",
            url: "cart_in",
            data: result,
            success: function() {
                alert("SUCCESS")
        }

        });

    }  

urls.py

url(r'^cart_in/(?P<qid>\w+)/$', cart_in ,name='cart_in'),

views.py

def cart_in(request, qid):
    id = qid
    print(id)
    return redirect('/')

我要传递的数组

[{"quantity":"36","customer_ID_id":1,"shop_ID_id":1,"product_ID_id":1},
 {"quantity":"3","customer_ID_id":1,"shop_ID_id":1,"product_ID_id":2}]

我想将上面提到的数组从'template'传递给'views.py',这样做时我被卡住了。我可能会遵循错误的方式。 帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

脚本

constructor(
        private pagePvdr: PageProvider,
        private wishlistPvdr: WishlistProvider,
        private hapticPvdr: HapticProvider,
        private alertCtrl: AlertController,
        private actionSheetCtrl: ActionSheetController,
        private navParams: NavParams,
        private navCtrl: NavController) {
        let postId = this.navParams.get('postId');
        this.currentIndex = this.navParams.get('index');
        this.pagePvdr._getPages(postId)
            .subscribe(p => {
                    this.pages = p;
                setTimeout(() => {
                this.updateSlide();
                }, 500);
            });

    }

views.py

   function submition(){
        var result=finalaproduct;
        alert(result)
        $.ajax({
            headers: { "X-CSRFToken": '{{csrf_token}}' },
            type: "POST",
            url: "/cart_in/",
            data:{"content": result},
            success:function(){
                alert("Data recieved")
            }

        });

    }

urls.py

def cart_in(request,qid,*args,**kwargs):
   data = request.POST['content']
   print(type(data))
   pass

这将起作用。 有或没有ID。