首页 > 解决方案 > “str”对象没有属性“get”?

问题描述

我尝试使用 Django 表单和 ajax 更新一些数据,问题是表单不接受 'POST['dat'],我不知道为什么?任何解决方案

jQuery:

 $.ajax({ 
            headers: {
               
                "X-CSRFToken": csrftoken
            },
              data: {'dat' : $('#form-one').serialize(),'id':id}, 
              type: 'POST', 
              url: '/commande/update2post/', 
              success: function(response) { 
                $.ajax({ 
            headers: {
               
                "X-CSRFToken": csrftoken
            },
  });

视图.py:


def update2post(request):
    

    if request.method == 'POST' and request.is_ajax:
        print(request.POST['id'])
        print(request.POST.get('id'))
        commande = get_object_or_404(Commande, id=request.POST['id'])
        das = request.POST['dat']
       
        form = Commande_Form2(data=request.POST['dat'],instance=commande)// the issue ...
        if form.is_valid():
         form.save()

表格.py

class Commande_Form2(forms.ModelForm):
    Date = forms.CharField(widget=forms.TextInput(attrs={'type': 'date'}))
    Date_validation = forms.CharField(widget=forms.TextInput(attrs={'type': 'date'}))

    class Meta:
        model = Commande
        fields = ('Date','Client','Numero_commande','Montant_HT','Montant_TVA','Montant_TTC','Type_Service','validation','Date_validation')



标签: jquerydjango

解决方案


推荐阅读