Gixxerrider
Gixxerrider

Reputation: 23

Django objects.get matching query does not exist

I'm trying to get all attributes of a single object. I keep getting a "Devices matching query does not exist." I just cannot figure out my issue.

Models.py

`class Devices(models.Model):
    category_id = models.ForeignKey(Category, on_delete=models.CASCADE)
    device_description = models.CharField(max_length=100)
    device_status = models.CharField(max_length=50)
    device_date = models.DateTimeField()
    device_user = models.CharField(max_length=50)`

Views.py

def view_status(request, pk=None): device = Devices.objects.get(pk=pk) return render(request, 'homesite/device_status.html', device)

urls.py

url(r'^viewstatus/$', views.view_status, name='ViewStatus'),

here is the url I use to call http://localhost:8000/homesite/viewstatus/?pk=1

device_satus.html

{% extends "base.html" %} 
{% block head %} 
<title>Device Status</title> 
{% endblock%} 
{% block body %}
<h3>Device Status Detail</h3>
{{ devices.device_description }}
{{ devices.device_status }}
{{devices.device_date|date:"Y-m-d H:m:s"}}
{% endblock %}

There are 4 records in my able so I know there is a match for PK=1.

Upvotes: 2

Views: 1808

Answers (3)

j-i-l
j-i-l

Reputation: 10957

Note, that this is not the usual way to build an URL for accessing a specific object. Below I present first the approach that integrates pk in the URI and second the one passing pk as a parameter.


1. Approach

Here you put the pk in the URI and request something like http://localhost:8000/homesite/viewstatus/1/. If you do so, you need to adapt your urls.py by specifying what part of the URI is the pk you want:

# urls.py
url(r'^viewstatus/(?P<pk>\d+)/$', views.view_status, name='ViewStatus'),

The way you wrote the view is fine:

def view_status(request, pk=None):
    if pk is not None:
        device = Devices.objects.get(pk=pk)
    else:
        device = None
    return render(request, 'homesite/device_status.html', {'device' : device})

Now, views.view_status will be called with both the request object and the pk as arguments and objects.get will behave as you expected, if the pk you put in the URI exists in you database.

Note that this is the preferred way to get an object.


2. Approach

In this case you pass the pk as a parameter, so call http://localhost:8000/homesite/viewstatus/?pk=1, for example. Now pk is a parameter of a GET request. In this case:

# urls.py
url(r'^viewstatus/$', views.view_status, name='ViewStatus'),

And the view only takes the request object as argument. Within the view you can get the pk as follows:

def view_status(request):
    pk = request.GET.get('pk', None)
    if pk is not None:
        device = Devices.objects.get(pk=int(pk))
    else:
        device = None
    return render(request, 'homesite/device_status.html', {'device' : device})

So in this case your view does not take any arguments other than the request object.


Another issue is in your view function: Django's shortcut render takes a dict object for the optional argument context. Currently you directly pass a Devices object. You need to update your return statement in view_status:

return render(request, 'homesite/device_status.html', {'device' : device})

Hope that helps!

Upvotes: 1

Vadim Vzorov
Vadim Vzorov

Reputation: 1

Try changing your view function:

def view_status(request):
        pk = request.GET['pk']
        device = Devices.objects.get(pk=pk)
        return render(request, 'homesite/device_status.html', device)

Let me know if it helps :)

Upvotes: 0

Gixxerrider
Gixxerrider

Reputation: 23

I get an error 'Devices' object is not iterable

urls.py

this is how the url is set up. url(r'^viewstatus/$', views.view_status, name='ViewStatus'),

but is should be like this url(r'^viewstatus/(?P<pk>\d+)/$', views.view_status, name='ViewStatus'), so that I can call like this correct? http://localhost:8000/homesite/viewstatus/1/

views.py

def view_status(request): pk = request.GET['pk'] device = Devices.objects.get(pk=pk) return render(request, 'homesite/device_status.html', device

so i need the corresponding views.py code to work with http://localhost:8000/homesite/viewstatus/1/

I've stared at this for hours so I know I'm missing something simple.

Upvotes: 0

Related Questions