2

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.

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

11 Comments

thanks, so I get the url piece i got my approach from another example. i didn't follow the views.py part. I've updated the url.py as you stated. I'm not sure how to modify the views.py to match
so I followed approach 1. I get this error still 'Devices' object is not iterable. do I need to update my model?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) def __str__(self): return self.device_description
@Gixxerrider could you copy/paste the line with the error so that we see in which line and which file the error occurs.
ok, I had to paste as an answer due to the length. see below.
@Gixxerrider ok thanks! So the error is not raised within the view. Could you please add the content of homesite/device_status.html to your question.
|
0

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.

Comments

0

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 :)

2 Comments

is pk not a string in this case?
edited the answer - should be request.GET['pk']. It will be a string. If you need an integer simple do something like that int(pk) - it will turn string into integer

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.