use*_*909 6 python django django-rest-framework
我的错误类似于django错误:__ init __()只需要1个参数(给定2个),但由于没有正确的答案,我现在无法找到解决方法.所以,我再问一次.
在views.py中我写道:
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)
Run Code Online (Sandbox Code Playgroud)
在urls.py中我写道:
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'),
)
Run Code Online (Sandbox Code Playgroud)
然后我运行python manage.py runserver,它向我显示一条错误消息:ImportError at/snippets /
No module named snippets
Run Code Online (Sandbox Code Playgroud)
但是我在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',
)
Run Code Online (Sandbox Code Playgroud)
错误跟踪是:
模板渲染时出错
在模板E:\ Python27\lib\site-packages\rest_framework\templates\rest_framework\base.html中,第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">
Run Code Online (Sandbox Code Playgroud)
小智 27
你忘了使用as_view()吗?
url(r'^snippets/$', snippets.views.SnippetListView.as_view()),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8723 次 |
| 最近记录: |