Django1.9属性错误在/书籍/类型对象'书籍'没有属性'对象'
问题描述:
我是初学者,我试图从数据库中获取数据。但我不知道为什么我得到这个错误,并完全卡在这里。我正在关注视频教程https://www.youtube.com/watch?v=_cPM7CgG-Fc。Django1.9属性错误在/书籍/类型对象'书籍'没有属性'对象'
错误:从书籍
AttributeError at /books/
type object 'Books' has no attribute 'Objects'
Request Method: GET
Request URL: http://127.0.0.1:8000/books/
Django Version: 1.9
Exception Type: AttributeError
Exception Value:
type object 'Books' has no attribute 'Objects'
Exception Location: F:\FYP\db2\testproject\books\views.py in index, line 7
Python Executable: C:\Python27\python.exe
Python Version: 2.7.8
Python Path:
['F:\\FYP\\db2\\testproject',
'C:\\Python27\\lib\\site-packages\\setuptools-7.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\networkx-1.9.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\decorator-3.4.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\sys-1.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\requests-2.5.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\beautifulsoup-3.2.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\beautifulsoup4-4.3.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\nltk-3.0.1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\lxml-3.4.1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\stripogram-1.5-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\html2text-2014.12.29-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\dom-0.6-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\termcolor-1.1.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\simplejson-3.6.5-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\xmltodict-0.9.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\mysql-0.0.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.5-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\textstat-0.1.6-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\utils-0.7-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\tweetpy-0.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\requests_oauthlib-0.5.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\oauthlib-0.7.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\tweepy-3.3.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\six-1.9.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\cython-0.22-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\numpy-1.9.2-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pytz-2015.4-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\python_dateutil-2.4.2-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pandas-0.16.1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pip-7.0.3-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\matplotlib-1.4.3-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pyparsing-2.0.3-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\html5lib-1.0b8-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pelican-3.6.3-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\unidecode-0.04.19-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\blinker-1.4-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\docutils-0.12-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pygments-2.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\jinja2-2.8-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\feedgenerator-1.7-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\markupsafe-0.23-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\mechanize-0.2.5-py2.7.egg',
'C:\\Windows\\SYSTEM32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Fri, 29 Apr 2016 11:26:32 +0000
文件目录:
model.py
from django.db import models
# Create your models here.
class Books(models.Model):
title = models.CharField(max_length=150)
author = models.CharField(max_length=100)
read = models.CharField(max_length=3)
def _unicode_(self):
return self.title + "/"+ self.author+ "/" + self.read
views.py
from django.shortcuts import render
from django.http import HttpResponse
from books.models import Books
# Create your views here.
def index(request):
list = Books.Objects.all()
return HttpResponse(list)
urls.py
from django.conf.urls import url,include
from . import views
urlpatterns = [
url(r'^$',views.index),
]
从根: urls.py
"""testproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Import the include() function: from django.conf.urls import url, include
3. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^books/', include('books.urls')),
]
答
请更改
def index(request):
booklist = Books.objects.all() #Objects changed to objects ;
return HttpResponse(booklist)
'objects',不'Objects'! – schwobaseggl
在'views.py'更改'list = Books.Objects.all()'到'list = Books.objects.all()。' – gplayer
谢谢你们。我花了一整天。但我没有得到真正的结果,但只有获得图书objectBooks ... \t 后这样做。任何想法? – user3508182