【问题标题】:Django Indentation errorDjango缩进错误
【发布时间】:2010-03-09 13:54:12
【问题描述】:

我是 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)

请帮我解决这个问题!提前致谢。

【问题讨论】:

    标签: indentation django-views


    【解决方案1】:

    很可能您将制表符与空格混合使用 - 使用其中一个或另一个进行缩进...最好是空格,根据以下内容:

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

    【讨论】:

    • 一定要喜欢重要的空白让 Python 程序“更具可读性”;-)
    【解决方案2】:

    首先找到发生此错误的行。完全删除该行。转到前一行的末尾,然后按 Enter。现在输入该行。

    【讨论】:

      猜你喜欢
      • 2014-10-02
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多