3

Im new to Django Framework and i'm taking an online course on LinkedIn Learning. I am using a newer version of python/django so I run into some syntax problems.

my python version is 3.5.4rc1. my django version is 1.11.4

I created a model in models.py for an inventory:

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

This is the code in my views.py file:

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

In the browser the localhost:8000 shows homepage as expected. localhost:8000/item/1/ gives error:

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

Please Help!

1 Answer 1

4
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
Sign up to request clarification or add additional context in comments.

3 Comments

Hello Max, thx for quick response! my cmd still prompts: SyntaxError invalid syntax. Anymore suggestions?
Shouldn't be any quotes here. (id=id).
Hallo Daniel. That is what the course origianally said. But is not working either. Course uses different (older) combination of python/django.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.