Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-rest-framework: __init__() takes exactly 1 argument (2 given)

my error is similar to django error: __init__() takes exactly 1 argument (2 given), but as there is no correct answer and I can not find way to solve it now. So, I ask again.

In views.py I write:

class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders its content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

class SnippetListView(APIView):
    queryset = Snippet.objects.all()

    def get(self, request, format=None):
        users = self.queryset
        serializer = SnippetSerializer(users, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):     
        serializer = SnippetSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
       else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

In urls.py I write:

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from snippets.views import *
urlpatterns = patterns('snippets.views',
    url(r'^snippets/$', SnippetListView.as_view()),
    url(r'^snippets/(?P<pk>[0-9]+)/$', 'snippets.views.SnippetDetailView'),
)

And then I run python manage.py runserver, and it shows me an error message: ImportError at /snippets/

No module named snippets

but I import snippets in setting.py

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'snippets',

)

the error track is:

Error during template rendering

In template E:\Python27\lib\site-packages\rest_framework\templates\rest_framework\base.html, error at line 51

No module named snippets
41  <li class="dropdown">
42  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
43  {{ user }}
44  <b class="caret"></b>
45  </a>
46  <ul class="dropdown-menu">
47  <li>{% optional_logout request %}</li>
48  </ul>
49  </li>
50  {% else %}
51  <li>{% optional_login request %}</li>   <----the error is here
52  {% endif %}
53  {% endblock %}
54  </ul>
55  </div>
56  </div>
57  </div>
58  {% endblock %}
59  
60  {% block breadcrumbs %}
61  <ul class="breadcrumb">
like image 457
user504909 Avatar asked Sep 25 '13 03:09

user504909


1 Answers

Did you forget to use as_view() ?

url(r'^snippets/$', snippets.views.SnippetListView.as_view()),
like image 72
ledzep2 Avatar answered Oct 24 '22 16:10

ledzep2