在hours_ahead处断言

时间:2012-07-23 08:13:35

标签: django assertion

当我使用assert false方法时,我收到错误:

assertionError at /time/plus/2/
No exception supplied
Request Method: GET
Request URL:    http://localhost:8000/time/plus/2/
Django Version: 1.4
Exception Type: AssertionError
Exception Location: C:\my\my\views.py in hours_ahead, line 18

这是我的代码:

from django.http import HttpResponse, Http404
import datetime

def hello(request):
    return HttpResponse("Hello World")

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>it is now %s.</body></html>" % now
    return HttpResponse(html)

def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
           raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    assert False
    html = "<html><body>In %s hours,it will be %s.</body></html>" % (offset, dt)
    return HttpResponse(html)

我编写了django part2的明确指南中的代码。但它得到了错误,我无法理解这段代码中的错误。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

断言False总是抛出异常,它用于测试。 more information on testing here

我想你可以删除那一行,你的功能应该按预期工作。