(Django 1.9版,Python 3.4.3版,使用虚拟环境 mkvirtualenv --python=/usr/bin/python3.4 django19
)
我一直非常密切地关注Django教程,直到第5部分没有任何问题:
https://docs.djangoproject.com/en/1.9/intro/tutorial05/
虽然严格来说,问题与第5部分没有关系,但请耐心等待。我在pythonanywhere框架中遵循教程,因为最终我想在那里托管一个网站。因此,我也在关注Django教程'由pythonanywhere提供:
https://help.pythonanywhere.com/pages/FollowingTheDjangoTutorial
虽然我在本教程第5部分的建议中没有预料到我的问题。现在问题 - 在Django教程中的重点是:
要检查错误是否确实存在,请使用管理员创建日期位于以后的问题,并使用shell
检查方法
在链接到shell之后,我尝试(按照链接中的指示)运行:
django-admin shell --plain
但是我收到以下错误消息
(django19)19:11 ~/mysite $ django-admin shell --plain
Traceback (most recent call last):
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 391, in execute saved_locale = translation.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 176, in get_language
return _trans.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 57, in __getattr__
if settings.USE_I18N:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
During handling of the above exception, another exception occurred:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 360, in run_from_argv
connections.close_all()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 230, in close_all
for alias in self:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 224, in __iter__
return iter(self.databases)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 157, in databases
self._databases = settings.DATABASES
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
此错误消息建议通过定义变量DATABASES
来设置mysite/settings.py
(在DJANGO_SETTINGS_MODULE
中设置)。所以,这就是我所做的:
export DJANGO_SETTINGS_MODULE="${PWD}/mysite/settings.py"
__init__.py
和mysite
目录中还有必需的mysite/mysite
个文件。但是现在当我运行django-admin
命令时,我得到了:
ImportError: No module named 'mysite/settings'
我以后找不到任何文档或帮助了。我错过了什么?我需要在其他地方设置一些特别的东西吗?我正确设置DJANGO_SETTINGS_MODULE
吗? ImportError
听起来好像应该将DJANGO_SETTINGS_MODULE
设置为mysite.settings
或类似的东西,但这也不起作用。
答案 0 :(得分:6)
创建项目后,使用manage.py
而不是django-admin
来运行命令:
python manage.py shell
manage.py
命令会为您处理设置。如果您使用django-admin
,则必须手动设置DJANGO_SETTINGS_MODULE
。 DJANGO_SETTINGS_MODULE
应该是Python模块,例如mysite.settings
,而不是像/path/to/mysite/settings.py
这样的文件。
答案 1 :(得分:1)
确保您的项目在python导入搜索路径中。试试这个
>>import sys
>>sys.path
如果未列出项目的路径/目录,则需要执行
>>sys.path.append('/path/to/my/django/project')
然后像这样分配DJANGO_SETTINGS_MODULE:
$export DJANGO_SETTINGS_MODULE=mysite.settings