AttributeError'Client'对象没有属性'get'

时间:2019-10-24 16:11:29

标签: python django model

执行视图功能后,我很可能遇到一个愚蠢的问题,但我无法解决

在我的Django应用程序的degug模式下,我得到:

AttributeError
Exception Value:    
'Client' object has no attribute 'get' 
(...)
Error during template rendering
In template (...)Comptabo/templates/base.html, error at line 0

当我在索引页面中单击客户名称时。我已经尝试了很多事情,并查看了相应的主题以及文档,但我不知道错误是从哪里来的。这与模板或urls文件无关。.如果有人有主意

views.py中的客户端功能:

def client(request, id = 0):
    if id:  
        client = Client.objects.get(id = id)
        form = ClientForm(client)
        factures = Facture.objects.filter(client = client)
        devis = Devis.objects.filter(client = client)
        form_fact = FactureForm()
        form_dev = DevisForm()
        return render(request,'client.html',{'client' : client
          , 'form' : form, 'factures': factures, 'devis': devis
          , 'formf': form_fact,'formd': form_dev})
    else:
        return redirect('/index')

models.py中的我的班级客户:

class Client(models.Model):
    nom = models.CharField(max_length = 30)
    adresse_voie = models.CharField(max_length = 30)
    adresse_code = models.IntegerField()
    adresse_ville = models.CharField(max_length = 30)
    adresse_pays = models.CharField(max_length = 30, default = "France")
    tel = models.CharField(max_length = 12)
    fax = models.CharField(max_length = 12, blank = True)
    mail = models.EmailField()
    def __str__(self):
        return self.nom

最糟糕的是,函数Client.objects.get(id = id)可以正常工作,因为在本地变量中我的客户变量包含了所请求的对象:

client = Client: ICE
devis = QuerySet []
factures = QuerySet []
form = ClientForm bound=True, valid=True, fields=(nom;adresse_voie;
       adresse_code;adresse_ville;adresse_pays;tel;fax;mail)
form_dev = DevisForm bound=False, valid=Unknown, fields=(prestations;total_HT;date)
form_fact = FactureForm bound=False, valid=Unknown, fields=(prestations;total_HT;paye;date)
id = 1
request = WSGIRequest: GET '/client/1/'

1 个答案:

答案 0 :(得分:0)

您应该已经发布了完整的追溯,所以我们可以看到错误的出处。但是这一行显然是错误的:

form = ClientForm(client)

表单的第一个位置参数是post数据。您可能是想

form = ClientForm(instance=client)