简体   繁体   中英

'list' object has no attribute 'get'

This is my first web service using Django rest framework.

This how my settigngs looks like

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

And urls.py

from django.conf.urls import patterns, include, url
from data import *

urlpatterns = patterns('',
    url(r'^Data/Orders/$', Orders.as_view(), name='Orders')
)

Error:

   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'

Your view's get method has to return a HttpResponse object (see examples in the documentation on using class based views ). You are currently returning a list which Django will have no idea what to do with.

You will probably also need to look at the documentation for what to pass to your HttpResponse .

Since you trying to build a web service using django rest framework. The Response object helps to render the correct content type as requested by the client unlike the normal HttpResponse.

In your views.py include:

from rest_framework.response import Response

and instead of just

return ORDERS

do:

return Response(ORDERS)

Replace

return ORDERS

With

return HttpResponse(ORDERS)

You need to import

from django.http import HttpResponse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM