对不起我的英语。我使用Django Rest Framework启动Django项目(我是Django和Python的初学者)。
我的项目:
# tree -I 'env'
.
├── api
│ ├── admin.py
│ ├── admin.pyc
│ ├── apps.py
│ ├── apps.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0001_initial.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── serializers.py
│ ├── serializers.pyc
│ ├── tests.py
│ ├── tests.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── db.sqlite3
├── hugs
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
└── manage.py
我的项目(拥抱)urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('api.urls')),
]
我的应用(api)urls.py:
from django.conf.urls import url
from . import views
urlpatterns = ['',
url(r'^snippets/$', views.snippet_list),
url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
]
我的应用视图(api / views.py):
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from api.models import Snippet
from api.serializers import SnippetSerializer
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)
@csrf_exempt
def snippet_list(request):
....
....
错误:
AttributeError: 'module' object has no attribute 'snippet_list'
Callstack:
# ./manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7fe26a8ba050>
Traceback (most recent call last):
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/html/web-hugs/developments/darksite/hugs/urls.py", line 21, in <module>
url(r'^', include('api.urls')),
File "/var/www/html/web-hugs/developments/darksite/env/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/html/web-hugs/developments/darksite/api/urls.py", line 5, in <module>
url(r'^snippets/$', views.snippet_list),
AttributeError: 'module' object has no attribute 'snippet_list'
提前感谢你! :)
美好的一天!
答案 0 :(得分:2)
snippet_list
:unindent it。
请注意,Django从1.8开始提供了一个JSONResponse类,不需要定义自己的类。