python - Django 管理员导致 AttributeError

标签 python django django-admin python-3.3

我正在使用 Django Book 学习 Django。我在装有 Mavericks 10.9 的 Macbook Pro 上运行 python3.3.3,当我启用管理站点时,我收到“发生服务器错误。请联系管理员。”在浏览器中,来自 Django 服务器的“AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'”错误。我已经检查(并发布)了我的 settings.py 和 urls.py 文件,没有发现任何问题...

我在 GIT 上找到了一个类似的项目 here ,但我认为它不适用于我身上发生的事情。我认为这可能是 Mavericks 的问题,所以我运行了所有 brew 更新和 django 的 pip 升级,但我仍然收到此错误...

有什么想法吗?

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:

urls.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)),
)

设置.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('\\','/'),
)

最佳答案

我在尝试运行驻留在我的 django 项目之外的测试模块时遇到了这个错误(django 似乎不支持这种做法):

$ tree
.
├── example_project
│   ├── example_app
│   │   ├── __init__.py
│   │   └── models.py
│   ├── __init__.py
│   ├── manage.py
│   └── project
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── test
    └── test_example_app.py

该错误是由于 django 在过程中的不同点采用不同的导入路径引起的,因此我通过附加到 sys.path 来解决它。我会在生产环境中犹豫是否这样做,但我可以接受测试中的一些 hackiness。

这是我在 test_example_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))

关于python - Django 管理员导致 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366055/

相关文章:

python - 将从文件读取的 True/False 值转换为 boolean 值

python - 需要快速更新 Django 模型与其他两个模型之间的差异

python - 创建与现有 python 环境相同的 anaconda 环境

python - 负载均衡器后面的 AWS EC2 上的 Django HTTP_HOST 错误

javascript - 在外部 javascript 文件/django 中使用 DOM 时出现问题

python - 在每个数据库查询中调用 Django to_python 函数

python - 给定二维列表和值库的所有值的加权平均值

ios - iOS 中的 Django 管理过滤器小部件

python - django - 无法使用用户凭据登录

Django DateTime 字段生成带 OUT 时区的时间戳字段