`来自django.conf导入设置`不起作用

时间:2015-04-13 19:17:26

标签: python django virtualenv pycharm

我已经更改了我的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 settings django-avatars文件中使用了conf.py。我在我的项目中测试了import语句,发现当我输入settings.时,PyCharm似乎无法从设置类中列出预期的成员变量。我的设置文件与PyCharm中的Django正确关联。

1 个答案:

答案 0 :(得分:0)

谢谢Aamir。

当我再次查看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('\\','/')

修正了它。