0

getting type error at /. It may be too localized but I have been racking my brain and searching the web for an answer with no luck. Thank you in advance for your help

 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'),
]

here is my views. 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

Please help.

4
  • Please post your views. Commented Sep 21, 2015 at 19:17
  • @RahulGupta - my bad, added views.py as well. Commented Sep 21, 2015 at 19:31
  • what happens if you take off the decorators? (@csrf_protect, @ensure_csrf_cookie) Commented Sep 21, 2015 at 20:29
  • I think you need to return something at the end of index or throw an http exception. Commented Sep 21, 2015 at 20:31

1 Answer 1

3

I think the problem is how you are defining your patterns.

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'),
]

As you can see in the django source for patterns, patterns is a function, so you need to call it, with (), not using [], which is the syntax to access an item in a sequence.

So you should do this instead:

urlpatterns = patterns(''
    # ...
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.