0

当我python manage.py runserver在 PowerShell 上运行时,出现以下错误:

File "C:\Python27\lib\site-packages\django\utils\importlib.py", line
__import__(name)
IndentationError: unexpected indent 

但是,我之前从未接触过该文件,当我在 Notepad++ 中打开该文件时,它会显示:

if name.startswith('.'):
    if not package:
        raise TypeError("relative imports require the 'package' argument")
    level = 0
    for character in name:
        if character != '.':
            break
        level += 1
    name = _resolve_name(name[level:], package, level)
    __import__(name)  #LINE 35
return sys.modules[name]`

似乎有什么问题?我正在使用 Python 2.7 和 Django 1.4.2 在 Windows Vista x32 上工作,感谢您的帮助。

这是下的数据库信息settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'demo',                      # Or path to database file if using sqlite3.
        'USER': '****',                      # Not used with sqlite3.
        'PASSWORD': '*****',                  # Not used with sqlite3.
        'HOST': '127.0.0.1',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.
    }
} 

我也在'django.contrib.admin'同一个文件中取消了注释。我url(r'^admin/', include(admin.site.urls)), 在“urls.py”中取消注释以启用管理员。这是urls.py我从中得到的python manage.py startproject

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'demo.views.home', name='home'),
# url(r'^demo/', include('demo.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),

)

4

2 回答 2

1

Using tab in Django is a bad practice. In order to fixed your problem you must use spaces only, I think 4 spaces. Try to backspace your codes and you'll see that it's tab indention, use only spaces.

于 2013-02-09T06:52:01.697 回答
0

我今天遇到了同样的错误。就像 Dombeypython -m tabnanny -v *报告了Clean bill of health我所有的文件一样。

我正在使用 git,所以我尝试运行 master 分支(以前运行良好),但我得到了相同的结果。

我不知道是什么触发了错误,但卸载重新安装 Django 为我解决了它。

于 2014-08-06T11:13:01.250 回答