Skip to main content
 首页 » 编程设计

python之django tutorial no module named staticfilespolls 错误

2025年05月04日33mate10pro

我之前完成了教程的第一部分,卡住了,意识到教程有多个版本,删除了 polls 和 mysite 目录,然后重新开始。但是,当我运行

$ python manage.py sql 投票 结果是:

     c:\Python27\Scripts\mysite>python manage.py sql polls 
Traceback (most recent call last): 
  File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 
399, in execute_from_command_line 
    utility.execute() 
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 
392, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 242, 
 in run_from_argv 
    self.execute(*args, **options.__dict__) 
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 280, 
 in execute 
    translation.activate('en-us') 
  File "C:\Python27\lib\site-packages\django\utils\translation\__init__.py", lin 
e 130, in activate 
    return _trans.activate(language) 
  File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", l 
ine 188, in activate 
    _active.value = translation(language) 
  File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", l 
ine 177, in translation 
    default_translation = _fetch(settings.LANGUAGE_CODE) 
  File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", l 
ine 159, in _fetch 
    app = import_module(appname) 
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 40, in im 
port_module 
    __import__(name) 
ImportError: No module named staticfilespolls 

在 settings.py 中

INSTALLED_APPS = ( 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles' 
    'polls', 
) 
 
DATABASES = { 
    'default': { 
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 

我不知道错误是从哪里来的。

请您参考如下方法:

INSTALLED_APPS 设置中的 django.contrib.staticfiles 后面缺少逗号:

INSTALLED_APPS = ( 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles',  # HERE 
    'polls', 
) 

如果没有逗号,python 会将 django.contrib.staticfiles 和 polls 字符串解释为单个字符串。

演示:

>>> s = ('what',  
...      'a', 
...      'wonderful' 
...      'world') 
>>> s 
('what', 'a', 'wonderfulworld') 
 
>>> s = ('what', 
...      'a', 
...      'wonderful', 
...      'world') 
>>> s 
('what', 'a', 'wonderful', 'world')