DoesNotExist at /room//

So I’m trying to create a simple CRUD in my project and Creating, updating and deleting is working but when I follow the link I receive this:

Seems to be an error in Message class, so:

The model:

class Message(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    product = models.ForeignKey(Room, on_delete=models.CASCADE)
    body = models.TextField()
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
       return self.body[0:50]

The view:

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

The URL:

urlpatterns= [
    path('', views.home, name='home'),
    path('room/<str:pk>/',views.room, name='room'),
    path('create-room/', views.createRoom, name='create-room'),
    path('update-room/<str:pk>', views.updateRoom, name='update-room'),
    path('delete-room/<str:pk>', views.deleteRoom, name='delete-room'),
]

The message is pretty clear. It’s not a problem with the class. It’s simply that you don’t have a Message object with pk = 20.

(Note for the future, the traceback error that you get in the console where you run runserver is usually of more value than a screen capture from the browser for errors.)

But I literally created a room that should have this pk :confused: I can’t see nothing wrong in the code

You may have a Room with pk = 20, but that’s not what the error message is telling you.

It’s saying you don’t have a Message with pk=20.

I have no idea how to solve it. I tried to changemessage = Message.objects.get(id=pk) to message = Message.objects.get() but it keeps returning the same error

What Message object are you trying to get?

Whatever I wrote to my object. Like: name - Blue shoes ; price - USD 59,99 ; message: “Best shoes I have bought”

You have a ManyToOne relationship from Message to Room. Are you looking to get all Message that is related to Room, or just one? And if just one, which one?

well, for room I just can submit one. look
Captura de tela 2024-01-15 223651

What I think it is is: I created some rooms without a description (message), so if I made room 10 without message, and room 11 with message, them room 11 have message 10 and them message 11 doesn’t exist

I have this problem that once an object is created with an ID, like id 20, and I delete it, the next object I create is 21, not replacing the previous ID. I guess it’s the problem

Nope, that’s got nothing to do with it at all. I don’t understand what it is you’re trying to model here, but there’s a chance that you’ve simply created the wrong models for what you’re trying to do.

You’ve got a model where you can have multiple Message that all refer to the same Room.

So you could have a Room with pk = 10, and Messages with pk 10, 12, 14, 18, 35, 47, and 99 that all refer to that room.

If that isn’t what you’re trying to achieve, then your models are poorly designed.

If that is what you’re trying to do, then you need to understand which Message you want to retrieve out of the 7 possible choices.

It’s a simple post, Like a tweet, with the username, title and the message that is the commentary the user wanted to post

it was working btw, I don’t know what’s wrong now, lol. I will create another project, this one is a mess

This one is my code for message model:
Captura de tela 2024-01-15 230000
if it helps

Also this error I didn’t understand what to do with:
Captura de tela 2024-01-15 230837

Please don’t post images of code or error messages. Post the text you want to share by copy/paste the text into the body of your post, between lines of ``` like you did for your code in your original post at the top.
Also, when sharing an error message, please copy the entire traceback.

1 Like