Python Django管理员导致AttributeError
我正在用Django书学习Django。我正在使用Mavericks 10.9在Macbook Pro上运行python3.3.3,当我启用管理站点时,我收到Django服务器发出的“AttributeError:'RegexURLResolver'对象没有属性”\urlconf\u module'”错误。我已经检查(并发布)了我的settings.py和url.py文件,没有发现任何问题 我在GIT上发现了一个类似的条目,但我不相信它适用于我身上发生的事情。我认为这可能是Mavericks的问题,所以我运行了所有brew更新和django的pip升级,但我仍然收到这个错误 有什么想法吗Python Django管理员导致AttributeError,python,django,django-admin,python-3.3,Python,Django,Django Admin,Python 3.3,我正在用Django书学习Django。我正在使用Mavericks 10.9在Macbook Pro上运行python3.3.3,当我启用管理站点时,我收到Django服务器发出的“AttributeError:'RegexURLResolver'对象没有属性”\urlconf\u module'”错误。我已经检查(并发布)了我的settings.py和url.py文件,没有发现任何问题 我在GIT上发现了一个类似的条目,但我不相信它适用于我身上发生的事情。我认为这可能是Mavericks的问
Porta-PuterTwo:LearningDjango arana$ python3 manage.py runserver
Validating models...
0 errors found
December 03, 2013 - 21:42:20
Django version 1.6, using settings 'LearningDjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Traceback (most recent call last):
File "/usr/local/lib/python3.3/site-packages/django/core/urlresolvers.py", line 339, in urlconf_module
return self._urlconf_module
AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.3/site-packages/django/core/handlers/base.py", line 101, in get_response
resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python3.3/site-packages/django/core/urlresolvers.py", line 318, in resolve
for pattern in self.url_patterns:
URL.py:
from django.conf.urls import patterns, include, url
from LearningDjango.views import currentDatetime
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'LearningDjango.views.home', name='home'),
url(r'^time/$', currentDatetime),
# url(r'^blog/', include('blog.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include(admin.site.urls)),
)
settings.py:
"""
Django settings for LearningDjango project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#6wow&islp6!6@+$9b%j9@981k^@i_uf8^=u%7gp@0b_^j^6t9'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'books',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'LearningDjango.urls'
WSGI_APPLICATION = 'LearningDjango.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'EST'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)
解决方案,在我看来很奇怪,是我在views.py文件中指定了根文件夹作为books.models导入的开始。这似乎不仅破坏了管理启动,而且它给出的错误没有给出太多(如果有的话)根本问题的指示。只有当我回到Python2.7时,错误消息才给了我一些问题根源的指示。
我猜这是典型的新手风格,因为现在很明显,站点的“根”应该是包含manage.py的文件夹,因此不应该在任何路径规范中。尽管这本书似乎没有说明…我在尝试运行驻留在django项目之外的测试模块时遇到了这个错误(django实践似乎不支持): 这个错误是由django在进程的不同点假设了不同的导入路径引起的,所以我通过附加到
sys.path
解决了这个问题。在生产环境中这样做我会犹豫不决,但我可以接受测试中的一些黑客行为
下面是我在test\u example\u app.py中得到的结果:
import os
import sys
import django
from django.conf import settings
from django.test import LiveServerTestCase
from django.test.utils import get_runner
class TestExampleApp(LiveServerTestCase):
...
if __name__ == '__main__':
sys.path.append(os.path.realpath('./example_project'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'example_project.project.settings'
django.setup()
sys.path.append(os.path.realpath('./example_project/project'))
testrunner = get_runner(settings)()
failures = testrunner.run_tests(['test_example_app'])
sys.exit(bool(failures))
我知道这是wierd,但是你能在设置中交换CommonMiddleware
和SessionMiddleware
,然后再试一次吗?你运行的是什么版本的django?谢谢Karthikr,但没有任何区别。我想可能是python 3的问题?我切换到了python 2.7.6,现在我得到了一些其他东西。(importorror at/admin/No module named books.models)我开始认为将我的models文件夹放在顶层文件夹中是不正确的。它应该在哪里?是否使用settings.py?
import os
import sys
import django
from django.conf import settings
from django.test import LiveServerTestCase
from django.test.utils import get_runner
class TestExampleApp(LiveServerTestCase):
...
if __name__ == '__main__':
sys.path.append(os.path.realpath('./example_project'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'example_project.project.settings'
django.setup()
sys.path.append(os.path.realpath('./example_project/project'))
testrunner = get_runner(settings)()
failures = testrunner.run_tests(['test_example_app'])
sys.exit(bool(failures))