2010-03-09 53 views
2

我是Django的新手,在教程中嘗試使用此代碼。但是現在我不能夠運行,因爲下面的錯誤我的程序:Django縮進錯誤

IndentationError at/
('unexpected indent', ('D:\\django_workspace\\django_bookmarks\\..\\django_bookmarks\\bookmarks\\views.py', 14, 4, ' return HttpResponse(output)\n')) 
Request Method: GET 
Request URL: http://localhost:8000/ 
Exception Type: IndentationError 
Exception Value:  
('unexpected indent', ('D:\\django_workspace\\django_bookmarks\\..\\django_bookmarks\\bookmarks\\views.py', 14, 4, ' return HttpResponse(output)\n')) 
Exception Location: D:\django_workspace\django_bookmarks\..\django_bookmarks\urls.py in <module>, line 2 
Python Executable: C:\Python26\python.exe 
Python Version: 2.6.4 
Python Path: ['D:\\django_workspace\\django_bookmarks', 'C:\\Python26', 'C:\\WINDOWS\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26\\lib\\site-packages'] 
Server time: Tue, 9 Mar 2010 19:18:32 + 

我views.py文件中的代碼是:

from django.http import HttpResponse, Http404 
from django.contrib.auth.models import User 
from django.template import Context 
from django.template.loader import get_template 

def main_page(request): 
    template = get_template('main_page.html') 
    variables = Context({ 
     'head_title': 'Django Bookmarks', 
     'page_title': 'Welcome to Django Bookmarks', 
     'page_body': 'Where you can store and share bookmarks!' 
}) 
output = template.render(variables) 
    return HttpResponse(output) 

def user_page(request, username): 
    try: 
     user = User.objects.get(username=username) 
    except: 
     raise Http404('Requested user not found.') 
     bookmarks = user.bookmark_set.all() 
     template = get_template('user_page.html') 
     variables = Context({ 
       'username': username, 
       'bookmarks': bookmarks 
}) 
output = template.render(variables) 
    return HttpResponse(output) 

請幫我糾正了這個問題!提前致謝。

回答

13

最有可能你是混合卡口與空間 - 使用一種或另一種爲您縮進...最好的空間,根據這個:

http://www.python.org/dev/peps/pep-0008/

+2

只需要愛的方式顯着的空白使得Python程序「更具可讀性」;-) – 2010-11-01 19:16:08

+0

是的,我從標籤切換到空格,它開始工作。 – 2012-02-04 04:29:28

0

首先定位上會出現此錯誤的行。完全刪除該行。轉到上一行的末尾,然後按回車鍵。現在輸入該行。