我正在做这个简单的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'
有人可以建议,这里有什么问题?
答案 0 :(得分:5)
django-oauth2-provider
是用python 2编写的,与你的python 3.4不兼容
答案 1 :(得分:1)
将此行添加到INSTALLED_APPS
'rest_framework',