【问题标题】:No module named 'backends'没有名为“后端”的模块
【发布时间】:2015-08-28 09:24:33
【问题描述】:

我正在做这个简单的 django 教程 http://www.madewithtea.com/simple-todo-api-with-django-and-oauth2.html

这是我的 settings.py 文件

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_q7lkj9bgdsdadsqx%kihv-tyf0ugn*vj8+6lbkds7ff5d&m1-b@837t'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

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

    # TODO
    'provider',
    'provider.oauth2',

    'todo',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'todosite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'todosite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'todo_db',
        'USER': 'root',
        'PASSWORD': 'root',
    }
}

# TODO
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES':
        ('rest_framework.authentication.OAuth2Authentication',
         'rest_framework.authentication.SessionAuthentication'),
    'DEFAULT_MODEL_SERIALIZER_CLASS':
        'rest_framework.serializers.ModelSerializer',
    'DEFAULT_PERMISSION_CLASSES':
        ('rest_framework.permissions.IsAdminUser',)
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

我正在使用 virtualenv,我使用这个命令来安装 django 和其他包

pip install django django_oauth2_provider djangorestframework markdown django-filter

这是我的 urls.py

from django.conf.urls import patterns, include, url
from todo import views

urlpatterns = patterns('',
    # Registration of new users
    # TODO
    url(r'^register/$', views.RegistrationView.as_view()),

    # endpoints
    url(r'^todos/$', views.TodosView.as_view()),
    url(r'^todos/(?P<todo_id>[0-9]*)$', views.TodosView.as_view()),

    # API authentication
    # TODO
    url(r'^oauth2/', include('provider.oauth2.urls', namespace='oauth2')),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)

这是我尝试使用python manage.py syncdb时遇到的错误

Traceback (most recent call last):
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/site-packages/django/apps/config.py", line 114, in create
    cls = getattr(mod, cls_name)
AttributeError: 'module' object has no attribute 'oauth2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/site-packages/django/apps/config.py", line 119, in create
    import_module(entry)
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/Users/user1/PycharmProjects/todo/my_env/lib/python3.4/site-packages/provider/oauth2/__init__.py", line 1, in <module>
    import backends
ImportError: No module named 'backends'

有人可以建议,这里有什么问题吗?

【问题讨论】:

  • 您是否安装了markdowndjango-filter?我不知道这是否能解决问题,但它说明在教程中安装这些库。
  • @Paco 是的,我安装了 markdown 和 django-filter。但还是一样。
  • 您的INSTALLED_APPS 中似乎遗漏了'rest_framework'
  • django-oauth2-provider 不是用 python 2 写的吗?你能在你的文件中验证 lib 是否已编译吗?
  • @SylvainBiehler 看起来你是完全正确的!该库不适用于 python3.4。你能把这个写成答案,所以我可以接受吗?谢谢!

标签: python django django-authentication


【解决方案1】:

django-oauth2-provider 是用 python 2 编写的,与你的 python 3.4 不兼容

【讨论】:

【解决方案2】:

将此行添加到INSTALLED_APPS

'rest_framework',

【讨论】:

  • 我把它加到settings.py里了,还是一样。谢谢建议
  • 确保site-packages/provider/oauth2/的路径中有backends.py文件
  • 感谢您的建议。问题是django-oauth2-provider不兼容python3.4
  • 我很高兴你找到了问题。
  • 感谢参与。
猜你喜欢
  • 1970-01-01
  • 2017-07-16
  • 2016-04-30
  • 2013-09-30
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2017-06-16
相关资源
最近更新 更多