2017-09-05 46 views
2

Django Frameworkの新機能で、LinkedIn Learningのオンラインコースを受講しています。私はpython/djangoの新しいバージョンを使用しているので、いくつかの構文上の問題があります。属性エラーメソッドのオブジェクトには属性がありません

私のpythonバージョンは3.5.4rc1です。

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, 
    }) 

で:これは私のviews.pyファイル内のコードがある

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

: 私のDjangoのバージョンは、私がインベントリのmodels.pyでモデルを作成した1.11.4

ですブラウザlocalhost:8000は期待どおりホームページを表示します。 はlocalhost: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

こんにちはマックス、速い応答のためのthx!私のcmdはまだプロンプトを表示します:SyntaxError invalid syntax。他の提案? – JustRay

+0

ここに引用符を入れてはいけません。 '(id = id)'です。 –

+0

私はコードを変更しました - ダニエルローズマン –

関連する問題