【问题标题】:missing 1 required positional argument: 'offset'缺少 1 个必需的位置参数:“偏移量”
【发布时间】:2013-09-04 20:36:10
【问题描述】:

这段代码无法执行:

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

它返回此错误消息:

hours_ahead() missing 1 required positional argument: 'offset'

我安装了 Python 3.3。我在这里有什么遗漏吗?

【问题讨论】:

  • 这个方法是怎么调用的? offset 参数没有被传递。
  • 你是怎么调用函数的?发布完整的回溯。
  • 这是 Django 视图吗?什么是 URL 路由配置?
  • 感谢您的回复,您提到了 url 配置,我可以看到我的正则表达式有问题:url(r'^time/plus/\d+/$', hours_ahead),
  • 试试这个:url(r'^time/plus/(\d+)/$', hours_ahead)

标签: python django python-3.x offset


【解决方案1】:

我变了:

url(r'^time/plus/\d+/$', hours_ahead)

到:

url(r'^time/plus/(\d+)/$', hours_ahead)

显然它解决了我的问题。

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2017-03-27
    • 2019-11-12
    • 2019-12-23
    相关资源
    最近更新 更多