2016-01-24 171 views
1

對不起,我的英語。我用Django Rest Framework啓動Django Project(我是Django & Python的初學者)。Django,AttributeError:'模塊'對象沒有屬性

我的項目:

# 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' 

調用堆棧:

# ./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

你需要提及的類中,snippet_list函數存在於urls.py –

+0

Thx Avinash,沒關係! :) – Crumbleszatan

回答

2

snippet_list不應該在JSONResponse裏面定義:unindent它。

請注意,自1.8以來,Django提供了一個JSONResponse類,不需要定義自己的。

+0

我被阻止...當我添加@classmethod函數之前我有另一個類似的錯誤上的另一個函數,這是不是在我的views.py – Crumbleszatan

+0

定義好吧,我不明白你有什麼想法使用classmethod 。我的建議是通過不加推測的方式將它從課堂中帶走。 –

+0

我嘗試......這是我在Python和Django上工作的一天。這很不幸並不簡單 – Crumbleszatan

相關問題