动态URL问题
区别
url(r'^time/plus/\d{1,2}/$', hours_ahead)
url(r'^time/plus/(\d{1,2})/$', hours_ahead)
圆括号的作用:URL模式中标识为参数
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函数有两个参数,offset代表的就是圆括号里面的参数,如果没有圆括号,会有缺少参数的错误提示