`from django.conf import settings` 无效

`from django.conf import settings` is not working

我更改了 manage.py 和 settings.py 并添加了一个 wsgi.py 文件,使我的 Django 1.4 项目更接近现代。这样做主要是因为 django_gunicorn 现在已弃用。我在 PyCharm 4.0.6 开发环境中使用 Python 2.7.9 和 Django 1.4.19。

我还将我的目录结构从平面结构更改为嵌套结构。

/gse
    manage.py
    /gse
        __init__.py
        settings.py
        urls.py
        wsgi.py
        ...
    /app1
    /app2

manage.py

from os import environ
import sys


if __name__ == "__main__":
    environ.setdefault("DJANGO_SETTINGS_MODULE", "gse.settings")
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

wsgi.py

import os


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

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我安装了 django-avatar 一段时间后,现在在我 运行 manage.py validate.

我在 PyCharm 中得到以下回溯:

Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.6\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\Lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\nu\gse\manage.py", line 23, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\__init__.py", line 443, in execute_from_command_line
utility.execute()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 371, in handle
return self.handle_noargs(**options)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\commands\validate.py", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\validation.py", line 30, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 158, in get_app_errors
self._populate()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 64, in _populate
self.load_app(app_name, True)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 88, in load_app
models = import_module('.models', app_name)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\avatar\models.py", line 23, in <module>
avatar_storage = get_storage_class(settings.AVATAR_STORAGE)()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\files\storage.py", line 149, in __init__
self.location = abspathu(self.base_location)
File "C:\Users\nu\gseVirtualEnv\lib\ntpath.py", line 487, in abspath
path = _getfullpathname(path)
TypeError: coercing to Unicode: need string or buffer, tuple found

Process finished with exit code 1

我注意到 from django.conf import settingsdjango-avatars conf.py 文件中使用。我在我的项目中测试了 import 语句,发现当我键入 settings. 时,PyCharm 似乎无法从设置 class 中列出预期的成员变量。我的设置文件在 PyCharm.

中与 Django 正确关联

谢谢阿米尔。

当我再次查看 settings.py 时,我看到了这个:

MEDIA_ROOT = (os.path.join(os.path.dirname(__file__), '..', 'media').replace('\','/'),)

AVATAR_STORAGE = MEDIA_ROOT + '\avatars'

将 MEDIA_ROOT 分配行更改为:

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '..', 'media').replace('\','/')

已修复。