python – ‘list’对象没有属性’get’
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – ‘list’对象没有属性’get’,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2792字,纯文字阅读大概需要4分钟。
内容图文
![python – ‘list’对象没有属性’get’](/upload/InfoBanner/zyjiaocheng/734/737914b25e534284a820910095f54355.jpg)
这是我第一次使用Django rest框架的Web服务.
这就是我的settigngs的样子
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework'
)
data.py:
from rest_framework.views import View
from rest_framework.response import Response
from rest_framework import status
ORDERS = [
['0', 'John', 'Apple'],
['1', 'John', 'Orange'],
['2', 'John', 'Lemon'],
['3', 'Jane', 'Apple'],
['4', 'Jane', 'Banana'],
['5', 'Bill', 'Pineapple'],
['6', 'Bob', 'Orange']
]
class Orders(View):
"""
Provides access to all orders within the system.
"""
def get(self, request):
"""
Return a list of all orders.
"""
return ORDERS
class CustomerOrders(View):
"""
Provides access to all orders for a specific customer.
"""
def get(self, request, customer):
"""
Return a list of all orders for a specific customer.
"""
customerOrders = []
for order in ORDERS:
if order[1] == customer:
customerOrders.append(order)
return customerOrders
class Order(View):
"""
Provides access to individual orders.
"""
def get(self, request, id):
"""
Return a specific order given it's ID.
"""
orderWithId = None
for order in ORDERS:
if order[0] == id:
orderWithId = order
break
return orderWithId
和urls.py
from django.conf.urls import patterns, include, url
from data import *
urlpatterns = patterns('',
url(r'^Data/Orders/$', Orders.as_view(), name='Orders')
)
错误:
Environment:
Request Method: GET
Request URL: http://localhost:8000/Data/Orders/
Django Version: 1.8.5
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "C:\python27\lib\site-packages\django\core\handlers\base.py" in get_response
223. response = middleware_method(request, response)
File "C:\python27\lib\site-packages\django\middleware\clickjacking.py" in process_response
31. if response.get('X-Frame-Options', None) is not None:
Exception Type: AttributeError at /Data/Orders/
Exception Value: 'list' object has no attribute 'get'
解决方法:
您的视图的get方法必须返回一个HttpResponse对象(请参阅documentation on using class based views中的示例).您目前正在返回一个列表,Django将不知道该怎么做.
您可能还需要查看您的HttpResponse的what to pass文档.
内容总结
以上是互联网集市为您收集整理的python – ‘list’对象没有属性’get’全部内容,希望文章能够帮你解决python – ‘list’对象没有属性’get’所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。