在 Python Shell 中创建对象,查询时无法识别
Created Object in Python Shell, not recognized when queried
目前正在为我正在做的一些工作创建一个 Django 站点。以下是相关的代码块:
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)
我使用以下方法创建了许多 设备对象 :
python manage.py shell
方法,一些示例类别是:风扇、开关、模块。所有类别也已设置为自己的类型Class。现在我有 5 个对象,我分配了一个粉丝类别,但是当我尝试去 url:
127.0.0.1:8000/device_categories/fans/
我遇到错误:
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.
当我进入 shell 时,我已经验证了这些对象存在,所以我不确定我做错了什么。我在这里发现了类似的问题,但我已经尝试了这些解决方案,但不幸的是它们不适用。感谢您的帮助
在您的视图中更改此行:
all_devices = Device.objects.get(category__device_category=device_type)
收件人:
all_devices = Device.objects.filter(category__device_category=device_type)
目前正在为我正在做的一些工作创建一个 Django 站点。以下是相关的代码块:
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)
我使用以下方法创建了许多 设备对象 :
python manage.py shell
方法,一些示例类别是:风扇、开关、模块。所有类别也已设置为自己的类型Class。现在我有 5 个对象,我分配了一个粉丝类别,但是当我尝试去 url:
127.0.0.1:8000/device_categories/fans/
我遇到错误:
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.
当我进入 shell 时,我已经验证了这些对象存在,所以我不确定我做错了什么。我在这里发现了类似的问题,但我已经尝试了这些解决方案,但不幸的是它们不适用。感谢您的帮助
在您的视图中更改此行:
all_devices = Device.objects.get(category__device_category=device_type)
收件人:
all_devices = Device.objects.filter(category__device_category=device_type)