2017-09-05 51 views
2

我剛剛接觸到Django Framework,並且正在學習LinkedIn Learning的在線課程。我正在使用更新版本的python/django,所以我遇到了一些語法問題。屬性錯誤方法對象沒有屬性

我的python版本是3.5.4rc1。 我的Django的版本是1.11.4

我創建了一個模型在models.py用於庫存:

class Item(models.Model): 
    titel = models.CharField(max_length=200) 
    omschrijving = models.TextField() 
    aantal = models.IntegerField() 

這是我views.py文件中的代碼:

from django.shortcuts import render 
from django.http import Http404 

from inventory.models import Item 

def index(request): 
    items = Item.objects.exclude(aantal=0) 
    return render (request, 'inventory/index.html', { 
     'items': items, 
    }) 
    return HttpResponse('<p>In index view</p>') 

def item_detail(request, id): 
    try: 
     item = Item.objects.get.id=(id) #THIS ONE CAUSES PROBLEM??? 
    except Item.DoesNotExist: 
     raise Http404('Dit item bestaat niet') 
    return render(request, 'inventory/item_detail.html', { 
     'item': item, 
    }) 

在瀏覽器localhost:8000按預期顯示主頁。 本地主機:8000 /項目/ 1 /給出了錯誤:

AttributeError at /item/1/ 
'method' object has no attribute 'id' 
Request Method: GET 
Request URL: http://localhost:8000/item/1/ 
Django Version: 1.11.4 
Exception Type: AttributeError 
Exception Value:  
'method' object has no attribute 'id' 
Exception Location: 
C:\Users\info_000\Desktop\django\mysite\inventory\views.py in item_detail, line 15 

請幫助!

回答

3
item = Item.objects.get(id=id) 
         ^^^ 
# id = field_name of primary key in your model as string ('id' on default) 
# id = your local variable 'id' from function signature 
+0

Hello Max,thx快速響應!我的cmd仍然會提示:SyntaxError語法無效。再建議? – JustRay

+0

這裏不應該有任何引號。 '(ID =標識)'。 –

+0

我改編了代碼 - thx @DanielRoseman –

相關問題