Reputation: 35
Currently creating a Django site for some work I'm doing. Here are the relevant code blocks:
portal/device_categories/models.py
from django.db import models
# Create your models here.
class Type(models.Model):
device_category = models.CharField(max_length=20)
def __str__(self):
return self.device_category
class Device(models.Model):
category = models.ForeignKey(Type, on_delete=models.CASCADE)
tms_code = models.CharField(max_length=5)
device_name = models.CharField(max_length=30)
device_count = models.CharField(max_length=3)
def __str__(self):
return "Device:" + self.device_name
portal/device_categories/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<device_type>[A-Za-z]+)/$', views.deviceList, name='deviceList'),
]
portal/device_categories/views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Type, Device
from django.template import loader
# Create your views here.
def index(request):
return HttpResponse("This is the Device Categories Page")
def deviceList(request, device_type):
all_devices = Device.objects.get(category__device_category=device_type)
template = loader.get_template('device_categories/index.html')
context = {
'all_devices': all_devices,
}
return render(request, template, context)
I have created numerous Device Objects using the:
python manage.py shell
methodology, some example categories are: fans, switches, modules. All the categories have also been set up as their own Type Class. Now I have 5 objects that I assigned a category of fans, but when I try to go to url:
127.0.0.1:8000/device_categories/fans/
I am getting error:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/device_categories/fans/
Django Version: 1.11.4
Python Version: 3.6.0
Installed Applications:
['network_resources.apps.NetworkResourcesConfig',
'device_categories.apps.DeviceCategoriesConfig',
'device_inventory.apps.DeviceInventoryConfig',
'on_call.apps.OnCallConfig',
'contact_info.apps.ContactInfoConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\users\wskidmor\Django\nocportal\device_categories\views.py" in deviceList
11. all_devices = Device.objects.get(category__device_category=device_type)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\models\manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django-1.11.4-py3.6.egg\django\db\models\query.py" in get
380. self.model._meta.object_name
Exception Type: DoesNotExist at /device_categories/fans/
Exception Value: Device matching query does not exist.
I have verified that the objects exist when I go into the shell so I'm not sure what I'm doing wrong. I have found similar questions posted on here but I have tried those solutions and they unfortunately didn't apply. Thank you for your help
Upvotes: 1
Views: 1021
Reputation: 1709
Change this line in your views:
all_devices = Device.objects.get(category__device_category=device_type)
To:
all_devices = Device.objects.filter(category__device_category=device_type)
Upvotes: 3