Home > front end >  AttributeError in Django - object has no attribute 'get'
AttributeError in Django - object has no attribute 'get'

Time:04-27

It is the code of my Room model

class Room(models.Model):
    #host
    #topic
    name=models.CharField(max_length=200)
    desccription=models.TextField(null=True, blank=True)
    #participants=
    updated=models.DateTimeField(auto_now=True)
    created=models.DateTimeField(auto_now_add=True)

and here in views.py , I'm trying to get id

def room(request, pk):
    room=Room.objects.get(id=pk)
    context={'room':room}
    return render(request, 'base/room.html', context)

but it shows error

AttributeError at /room/1/
'Room' object has no attribute 'get'
Request Method: GET
Request URL:    http://127.0.0.1:8000/room/1/
Django Version: 4.0.4
Exception Type: AttributeError
Exception Value:    
'Room' object has no attribute 'get'

CodePudding user response:

That error can occur when models and views have similar names and there is a case error in urls.py. Check that you haven't used 'Room' with a capital 'R' in urls.py when you want to use 'room' with a lowercase 'r', eg you've used something like

path('', views.Room, name='room')

which should be

path('', views.room, name='room')
  • Related