我是Django Framework的新手,我正在学习LinkedIn学习的在线课程。我正在使用更新版本的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按预期显示主页。 localhost:8000 / item / 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
请帮助!
答案 0 :(得分: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