简体   繁体   English

关于pythonanywhere的Django教程:运行django-admin shell时遇到麻烦

[英]Django tutorial on pythonanywhere: trouble running the django-admin shell

(Django version 1.9, Python version 3.4.3, using virtual env made with mkvirtualenv --python=/usr/bin/python3.4 django19 ) (Django版本1.9,Python版本3.4.3,使用虚拟环境使用 mkvirtualenv --python=/usr/bin/python3.4 django19

I've been following the Django tutorial very closely, without any problems up til part 5: 我一直非常密切地关注Django教程,直到第5部分没有任何问题:

https://docs.djangoproject.com/en/1.9/intro/tutorial05/ https://docs.djangoproject.com/en/1.9/intro/tutorial05/

although, strictly speaking, the problem isn't to do with part 5, but bear with me. 虽然严格来说,问题与第5部分无关,但请耐心等待。 I'm following the tutorial in the pythonanywhere framework, because eventually I would like to host a website there. 我正在遵循pythonanywhere框架中的教程,因为最终我想在那里托管一个网站。 Therefore, I am also following the 'Following the Django tutorial' offered by pythonanywhere: 因此,我也在关注pythonanywhere提供的“关注Django教程”:

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

although my problem isn't anticipated in the advice for part 5 in the tutorial. 虽然在教程的第5部分的建议中没有预料到我的问题。 Now the problem- at the point in the Django tutorial which reads: 现在问题 - 在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 要检查错误是否确实存在,请使用Admin创建一个日期位于将来的问题,并使用shell检查方法

Following the link to the shell, I try (as instructed in the link) to run: 在链接到shell之后,我尝试(按照链接中的指示)运行:

django-admin shell --plain

However I get the following error message 但是,我收到以下错误消息

(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.

This error messages recommends setting DATABASES (which is set in mysite/settings.py ), by defining the variable DJANGO_SETTINGS_MODULE . 此错误消息建议通过定义变量DJANGO_SETTINGS_MODULE设置DATABASES (在mysite/settings.py )。 So, that's what I did: 那就是我做的:

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

There are also the required __init__.py files in the mysite and mysite/mysite directories. mysitemysite/mysite目录中还有所需的__init__.py文件。 However now when I run the django-admin command I get: 但是现在当我运行django-admin命令时,我得到:

ImportError: No module named 'mysite/settings'

I can't find anymore documentation or help hereafter. 我以后找不到任何文档或帮助。 What am I missing? 我错过了什么? Do I need to set something special somewhere else? 我需要在其他地方设置一些特别的东西吗? Am I setting DJANGO_SETTINGS_MODULE correctly? 我正确设置了DJANGO_SETTINGS_MODULE吗? The ImportError makes it sound like I should set DJANGO_SETTINGS_MODULE to mysite.settings or something similar, but this doesn't work either. ImportError听起来像我应该将DJANGO_SETTINGS_MODULE设置为mysite.settings或类似的东西,但这也不起作用。

Once you have created your project, use manage.py instead of django-admin to run commands: 创建项目后,使用manage.py而不是django-admin来运行命令:

python manage.py shell

The manage.py command takes care of the settings for you. manage.py命令负责处理您的设置。 If you use django-admin , you have to set DJANGO_SETTINGS_MODULE manually. 如果您使用django-admin ,则必须手动设置DJANGO_SETTINGS_MODULE The DJANGO_SETTINGS_MODULE should be the Python module, eg mysite.settings , not a file like /path/to/mysite/settings.py . DJANGO_SETTINGS_MODULE应该是Python模块,例如mysite.settings ,而不是像/path/to/mysite/settings.py这样的文件。

Be sure to have your project in the python import search path. 一定要让你的项目在python导入搜索路径中。 Try this 尝试这个

>>import sys
>>sys.path

if the path/directory for your project is not listed, then you need to do 如果未列出项目的路径/目录,则需要执行此操作

>>sys.path.append('/path/to/my/django/project')

Then assign DJANGO_SETTINGS_MODULE like so: 然后像这样分配DJANGO_SETTINGS_MODULE:

$export DJANGO_SETTINGS_MODULE=mysite.settings

See Designating Django settings 请参阅指定Django设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM