2016-01-21 101 views
2

(Django的版本1.9,Python版本3.4.3,使用帶有mkvirtualenv --python=/usr/bin/python3.4 django19發虛ENV)Django的教程pythonanywhere:麻煩運行Django的管理員外殼

我一直在關注Django的教程非常緊密,沒有任何問題,不外第5部分:

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

雖然嚴格來說,問題不在於做第5部分,但我承擔。我遵循pythonanywhere框架中的教程,因爲最終我想在那裏主持一個網站。因此,我也繼「繼Django的教程」由pythonanywhere提供:

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

雖然我的問題是不是在建議預計在本教程第5部分。現在,在Django的教程點problem-其內容爲:使用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" 

也有所需__init__.py文件在mysitemysite/mysite目錄。但是現在,當我運行django-admin命令我得到:

ImportError: No module named 'mysite/settings' 

我再也找不到文件或以後幫助。我錯過了什麼?我需要在其他地方設置特別的東西嗎?我正確設置DJANGO_SETTINGS_MODULE嗎? ImportError聽起來像我應該設置DJANGO_SETTINGS_MODULEmysite.settings或類似的東西,但這也不起作用。

回答

5

一旦你創建你的項目,使用的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這樣的文件。

1

一定要在Python導入搜索路徑中有您的項目。試試這個

>>import sys 
>>sys.path 

如果未列出您的項目的路徑/目錄,那麼你需要做的

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

然後分配DJANGO_SETTINGS_MODULE像這樣:

$export DJANGO_SETTINGS_MODULE=mysite.settings 

Designating Django settings