NoReverseMatch:带有参数'()'和关键字参数

时间:2013-08-06 00:07:47

标签: django django-templates

我正在使用Django 1.5并尝试将args传递给我的URL。当我使用前两个args然后下面的代码工作正常,第三个args我得到一个错误。我已经提到了url用法的新Django 1.5更新,因此使用了URL名称的引号。

NoReverseMatch: Reverse for 'add_trip' with arguments '()' and keyword arguments '{u'city': 537, u'score': 537, u'time': 35703, u'distance': 1196.61}' not found

urls.py

url(
    r'^add/(?P<city>\w+)/(?P<score>\w+)/(?P<distance>\w+)/(?P<time>\w+)$',
    'trips.views.add_trip',
    name='add_trip'
),

html文件

<a href="{% url "add_trip" city=data.city score=data.score distance=data.distance time=data.time%}">Add/delete</a>

如果我只使用两个args(即city和score,那么它工作正常),否则我得到no reverse match错误。

views.py

def SearchTrips(request):
    city = request.POST['city'].replace(" ","%20")
    location = request.POST['location'].replace(" ","%20")
    duration = request.POST['duration']
    #url = "http://blankket-mk8te7kbzv.elasticbeanstalk.com/getroutes?city=%s&location=%s&duration=%s" % (city, location, duration)
    url= "http://blankket-mk8te7kbzv.elasticbeanstalk.com/getroutes?city=New%20York%20City&location=Park%20Avenue&duration=10"
    print url

    try:
        resp = urllib2.urlopen(url)
    except:
        resp = None

    if resp:
        datas = json.load(resp)
    else:
        datas = None

    return render(request, 'searchtrips.html', {'datas': datas})

1 个答案:

答案 0 :(得分:0)

由于小数点,距离值1196.61与正则表达式不匹配。

您可以使用

(?P<distance>[\w\.]+)

匹配大写的A-Z,小写的a-z,数字0-9,连字符和小数点。

或者,您可以使用

(?P<distance>[\d\.]+)

仅匹配数字0-9和小数点。

相关问题