在/获取类型错误。它可能过于局部化,但我一直在绞尽脑汁,在网上搜索没有运气的答案。提前感谢您的帮助
TypeError at /
'function' object has no attribute '__getitem__'
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.3
Exception Type: TypeError
Exception Value:
'function' object has no attribute '__getitem__'
Exception Location: /Users/wrb473/Documents/workspace/angulardjango/angulardjango/urls.py in <module>, line 22
Python Executable: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.10
url.py
from django.conf.urls import patterns, include, url
from rest_framework.routers import DefaultRouter
#from rest_framework import routers
from django.contrib import admin
from django.contrib.auth.models import User
from rest_framework import routers,serializers,viewsets
from tweeter import views
admin.autodiscover()
router = DefaultRouter()
router = routers.SimpleRouter()
router.register(r'tweets', views.TweetViewSet,base_name='tweets')
router.register(r'users', views.UserViewSet,base_name='users')
urlpatterns = patterns['',
url(r'^admin/', include(admin.site.urls)),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', views.index, name='index'),
]
这是我的观点。 views.py
from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_protect, ensure_csrf_cookie
from rest_framework import permissions, viewsets
from tweeter.models import Tweet
from tweeter.permissions import IsAuthorOrReadOnly
from tweeter.serializers import TweetSerializer, UserSerializer
@csrf_protect
@ensure_csrf_cookie
def index(request):
user = authenticate(username='bob', password='bob')
if user is not None:
login(request, user)
return render(request, 'tweeter/index.html')
class TweetViewSet(viewsets.ModelViewSet):
queryset = Tweet.objects.all()
serializer_class = TweetSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsAuthorOrReadOnly,)
def pre_save(self, obj):
obj.user = self.request.user
class UserViewSet(viewsets.ReadOnlyModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
请帮忙。
答案 0 :(得分:3)
我认为问题在于你如何定义模式。
urlpatterns = patterns['',
url(r'^admin/', include(admin.site.urls)),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', views.index, name='index'),
]
正如你在django source for patterns中看到的那样, patterns
是一个功能,所以你需要调用,{{1不使用()
,这是访问序列中项目的语法。
所以你应该这样做:
[]