每当我在Python GUI中输入此命令时:
from django.db import models
它给我一个像这样的错误
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
from django.db import models
File "C:\Python27\lib\site-packages\django\db\models\__init__.py", line 5, in <module>
from django.db.models.query import Q
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 17, in <module>
from django.db.models.deletion import Collector
File "C:\Python27\lib\site-packages\django\db\models\deletion.py", line 4, in <module>
from django.db.models import signals, sql
File "C:\Python27\lib\site-packages\django\db\models\sql\__init__.py", line 4, in <module>
from django.db.models.sql.subqueries import *
File "C:\Python27\lib\site-packages\django\db\models\sql\subqueries.py", line 12, in <module>
from django.db.models.sql.query import Query
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 22, in <module>
from django.db.models.sql import aggregates as base_aggregates_module
File "C:\Python27\lib\site-packages\django\db\models\sql\aggregates.py", line 9, in <module>
ordinal_aggregate_field = IntegerField()
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 116, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
这是什么问题?我在目录C:\Python27\Lib\site-packages\django\db
中有django,在那里我有一个名为models的文件。
这是图片
奇怪不是吗?
答案 0 :(得分:1)
不幸的是,并非所有Django都支持简单地作为Python模块加载;你需要使用Django本身提供的方法来引导加载像模型这样的东西所需的环境。
一种选择是使用Django shell,但也可以使用自己代码中的纯编程解决方案。有关所有选项,请参阅https://docs.djangoproject.com/en/dev/topics/settings/。
以下是其他人https://gist.github.com/jordanorelli/1025419的示例,但我还没有确认它是否是最新的。
答案 1 :(得分:0)
答案在错误消息中:
You must either define the environment variable DJANGO_SETTINGS_MODULE or...
因此,Django提供了一个快捷方式来加载配置了Django设置的python shell:
https://docs.djangoproject.com/en/dev/ref/django-admin/#shell