gpt4 book ai didi

python - 关于 pythonanywhere : trouble running the django-admin shell 的 Django 教程

转载 作者:行者123 更新时间:2023-11-28 20:40:54 25 4
gpt4 key购买 nike

(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 框架中的教程进行操作,因为最终我想在那里托管一个网站。因此,我也在关注 pythonanywhere 提供的“Following the Django tutorial”:

https://help.pythonanywhere.com/pages/FollowingTheDjangoTutorial

虽然教程第 5 部分的建议中没有预料到我的问题。现在是问题所在——Django 教程中的一点:

To check if the bug really exists, using the Admin create a question whose date lies in the future and check the method using the 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.

此错误消息建议通过定义变量 DJANGO_SETTINGS_MODULE 来设置 DATABASES(在 mysite/settings.py 中设置)。所以,这就是我所做的:

export DJANGO_SETTINGS_MODULE="${PWD}/mysite/settings.py"

mysitemysite/mysite 目录中也有所需的__init__.py 文件。但是现在当我运行 django-admin 命令时,我得到:

ImportError: No module named 'mysite/settings'

此后我找不到更多文档或帮助。我错过了什么?我需要在其他地方设置一些特别的东西吗?我是否正确设置了 DJANGO_SETTINGS_MODULEImportError 听起来我应该将 DJANGO_SETTINGS_MODULE 设置为 mysite.settings 或类似的东西,但这也不起作用。

最佳答案

创建项目后,使用 manage.py 而不是 django-admin 来运行命令:

python manage.py shell

manage.py 命令会为您处理设置。如果你使用 django-admin,你必须手动设置 DJANGO_SETTINGS_MODULEDJANGO_SETTINGS_MODULE 应该是 Python 模块,例如mysite.settings,而不是像 /path/to/mysite/settings.py 这样的文件。

关于python - 关于 pythonanywhere : trouble running the django-admin shell 的 Django 教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34933208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com