Python 配置 Django

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20794901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:15:05  来源:igfitidea点击:

Configuring Django

pythondjangomacosdjango-settings

提问by theNinja

I just installed Django 1.6 on OS X 10.8 with python 2.7 and am having trouble going through the tutorial. After setting creating a server called mysite by running:

我刚刚使用 python 2.7 在 OS X 10.8 上安装了 Django 1.6,但在阅读本教程时遇到了问题。通过运行设置创建名为 mysite 的服务器后:

django-admin.py startproject mysite

I then cd'ed into mysite and ran

然后我 cd 进入 mysite 并运行

python manage.py runserver

and got this error:

并收到此错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 280, in execute
    translation.activate('en-us')
  File "/Library/Python/2.7/site-packages/django/utils/translation/__init__.py", line 130, in activate
    return _trans.activate(language)
  File "/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py", line 188, in activate
    _active.value = translation(language)
  File "/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py", line 177, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch
    app = import_module(appname)
  File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/Library/Python/2.7/site-packages/django/contrib/admin/__init__.py", line 6, in <module>
    from django.contrib.admin.sites import AdminSite, site
  File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py", line 4, in <module>
    from django.contrib.admin.forms import AdminAuthenticationForm
  File "/Library/Python/2.7/site-packages/django/contrib/admin/forms.py", line 6, in <module>
    from django.contrib.auth.forms import AuthenticationForm
  File "/Library/Python/2.7/site-packages/django/contrib/auth/forms.py", line 17, in <module>
    from django.contrib.auth.models import User
  File "/Library/Python/2.7/site-packages/django/contrib/auth/models.py", line 361, in <module>
    class AbstractUser(AbstractBaseUser, PermissionsMixin):
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 212, in __new__
    new_class.add_to_class(field.name, copy.deepcopy(field))
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 264, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 303, in contribute_to_class
    cls._meta.add_field(self)
  File "/Library/Python/2.7/site-packages/django/db/models/options.py", line 172, in add_field
    self.local_fields.insert(bisect(self.local_fields, field), field)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
    '__lt__': [('__gt__', lambda self, other: other < self),
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
.........

this error ends up being repeatedly printed until the end:

这个错误最终会被重复打印,直到结束:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in '_lt_': [('_gt_', lambda self, other: other < self),

文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py”,第 56 行,'_ lt_':[('_ gt_', lambda self, other : 其他 < 自己),

And here are the last few lines of output:

这是输出的最后几行:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
    '__lt__': [('__gt__', lambda self, other: other < self),
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
    '__lt__': [('__gt__', lambda self, other: other < self),
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
    '__lt__': [('__gt__', lambda self, other: other < self),
RuntimeError: maximum recursion depth exceeded while calling a Python object

After looking at the last line:

查看最后一行后:

RuntimeError: maximum recursion depth exceeded while calling a Python object

I also ran:

我也跑了:

django-admin.py check

and got this output:

并得到这个输出:

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I also tried setting my environment variable in tcsh by typing in:

我还尝试通过键入以下内容在 tcsh 中设置我的环境变量:

setenv DJANGO_SETTINGS_MODULE "mysite.settings"

but it still gave me the recursion depth error. Running

但它仍然给了我递归深度错误。跑步

django-admin.py check

then gave me this error:

然后给了我这个错误:

ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mysite.settings

Here is my settings.py:

这是我的settings.py:

"""
Django settings for mysite 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 = '3e%g^*y!3=ln75%usf2ftjf&urt7&kuj@_-yc+53pjm)$@9^'

# 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.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

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.clickHymaning.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.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'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/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.6/howto/static-files/

STATIC_URL = '/static/'

And my file structure:

我的文件结构:

mysite/
 manage.py
 mysite/
  __init__.py
  settings.py
  urls.py
  __init__.pyc
  settings.pyc
  wsgi.py

I am a newbie at django and am really stuck. Can anyone offer some insight?

我是 Django 的新手,我真的被困住了。任何人都可以提供一些见解吗?

采纳答案by theNinja

Ok so I finally found the answer. Apparently the error was a result of Python 2.7.2. Once I upgraded to 2.7.6, the problem went away.

好的,所以我终于找到了答案。显然该错误是 Python 2.7.2 的结果。一旦我升级到 2.7.6,问题就消失了。

Here is a similar thread where I found the answer:

这是我找到答案的类似线程:

maximum recursion depth exceeded in cmp error while executing python manage.py runserver

执行 python manage.py runserver 时 cmp 错误中超出了最大递归深度

Don't know why this solution didn't show up in my searches when I was creating this thread...

不知道为什么当我创建这个线程时这个解决方案没有出现在我的搜索中......

回答by Marius Darila

Check in manage.pyif you have this:

manage.py如果你有这个,请检查:

import os

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

And in wsgi.pythis:

而在wsgi.py这方面:

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

Also, any python scripts that integrate Django should look like this:

此外,任何集成 Django 的 Python 脚本都应如下所示:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
# Uncomment below for Django 1.7 +
#import django
#django.setup()