DRF 1.7.1'str'对象没有属性'resolve'NameError

时间:2015-03-18 11:14:06

标签: python django python-2.7

我试图让我的第一个DRF api正常工作,但当我尝试使用curl或网络浏览器发出GET请求时,即我正在使用' str'对象没有属性' resolve' AttributeError的。
我用于测试的地址是:http://0.0.0.0:8000/polls/expediteur/

我在下面留下一些代码段。

urls.py

from django.conf.urls import patterns, url
from polls import views
# from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^admin/', include(admin.site.urls)),
    "polls.view",
    url(r'^Expediteur/$', views.ExpeViewSet.as_view(), name="expediteur"),
)

models.py

from django.db import models

# Create your models here.

class Expediteur(models.Model):
    nom = models.CharField(max_length=50)
    prenom = models.CharField(max_length=100)
    adresse = models.CharField(max_length=200)
    tel = models.IntegerField()

views.py

from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from polls.serializers import ExpediteurSerializer, DestinataireSerializer, LettrePrioSerializer, TypeLettreRecoSerializer, LettreRecoSerializer, TimbrePrioSerializer, TimbreRecoSerializer
from polls.models import Expediteur, Destinataire, LettrePrio, TypeLettreReco, LettreReco, TimbrePrio, TimbreReco
from rest_framework import status, HTTP_HEADER_ENCODING
from django.shortcuts import render, get_object_or_404

import json
import datetime

from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.shortcuts import render, get_object_or_404
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth import login
from django.core.exceptions import ObjectDoesNotExist
from django.views.decorators.csrf import csrf_exempt
from django.utils.timezone import get_current_timezone

# Create your views here.

class ExpeViewSet(APIView):
    serializer_class = ExpediteurSerializer

    def get(self, request):
        queryset = Expediteur.objects.all()
        serializer = ExpediteurSerializer(queryset)
        return Response(serializer.data)

    def post(self, request):
        serializer = self.serializer_class(data=request.DATA)
        return Response(status=status.HTTP_201_CREATED)

serializers.py

from rest_framework import serializers
from polls.models import Expediteur, Destinataire, LettrePrio, TypeLettreReco, LettreReco, TimbrePrio, TimbreReco


class ExpediteurSerializer(serializers.Serializer):
    nom = serializers.CharField(required=True, allow_blank=False, max_length=50)
    prenom = serializers.CharField(required=True, allow_blank=False, max_length=100)
    adresse = serializers.CharField(required=True, allow_blank=False, max_length=200)
    tel = serializers.IntegerField(required=True)

    def create(self, validated_data):
        return Expediteur.objects.create(**validated_data)
    def update(self, instance, validated_data):
        instance.nom = validated_data.get('nom', instance.nom)
        instance.prenom = validated_data.get('prenom', instance.prenom)
        instance.adresse = validated_data.get('adresse', insatnce.adresse)
        instance.tel = validated_data.get('tel', instance.tel)
        instance.save()
        return instance

已经有两个ExpediteurObject,我的目标是能够在移动应用中与它进行互动。我已查看'str' object has no attribute 'resolve' when access admin sitehttp://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/

看看我是否可以解决问题,但我没有找到问题的答案。

我也留下了追溯

回溯

Environment:


Request Method: GET
Request URL: http://0.0.0.0:8000/polls/expediteur/

Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'polls')
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')


Traceback:
File "/Library/Python/2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/handlers/base.py" in get_response
  98.                 resolver_match = resolver.resolve(request.path_info)
File "/Library/Python/2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/urlresolvers.py" in resolve
  345.                     sub_match = pattern.resolve(new_path)

Exception Type: AttributeError at /polls/expediteur/
Exception Value: 'str' object has no attribute 'resolve'

1 个答案:

答案 0 :(得分:1)

"polls.view",

中删除patterns()字符串
urlpatterns = patterns('',
    # Examples:
    # url(r'^admin/', include(admin.site.urls)),
    url(r'^Expediteur/$', views.ExpeViewSet.as_view(), name="expediteur"),
)

要访问/polls/expediteur/地址的视图,请将网址更改为:

url(r'^polls/expediteur/$', views.ExpeViewSet.as_view(), name="expediteur"),

或者,如果此urls.py来自polls应用:

url(r'^expediteur/$', views.ExpeViewSet.as_view(), name="expediteur"),