Django缩进错误 [英] Django Indentation error

查看:105
本文介绍了Django缩进错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,正在教程中尝试此代码。但是现在由于以下错误,我无法运行我的程序:

I am new to Django and was trying this code in a tutorial. But now I'm not able to run my program because of the following error:

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 +

我的意见。 py文件的代码是:

My views.py file code is:

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)

请帮助我纠正此问题!

Kindly help me rectify this problem! Thanks in advance.

推荐答案

最有可能是您在将制表符与空格混合-缩进使用一个或多个...最好是空格,据此:

Most likely you're mixing tabs with spaces - use one or the other for your indenting... preferably spaces, according to this:

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

这篇关于Django缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆