1

Excuse me for my english. I start Django Project (I'm beginner on Django & Python) with Django Rest Framework.

My project :

# 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

My project (Hugs) 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')),
]

My app (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),
]

My app views (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):
        ....
        ....

Error :

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'

Thanking you in advance ! :)

Good day !

2
  • you need to mention also the class where the snippet_list func exists in urls.py Commented Jan 24, 2016 at 14:51
  • Thx Avinash, it's ok ! :) Commented Jan 24, 2016 at 15:08

1 Answer 1

2

snippet_list should not be defined inside JSONResponse: unindent it.

Note that Django has provided a JSONResponse class since 1.8, there is no need to define your own.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm blocked ... when i add @classmethod before function i have another similar error on another function which isn't define in my views.py
Well I can't see where you got the idea of using classmethod. My advice was to take it out of the class by unindenting it.
I try ... it's one day I work on Python and Django. This is unfortunately not simple
This is perfectly simple. Move the function out of the class by moving it one indent to the left.
Daniel, thank you for your help & sorry for my stupid question ... I have other error (ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class) but i'll look. Thank you very much Daniel.
|

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.