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

时间:2021-12-10 23:21:06

I am using Django 1.5 and trying to pass args to my URL. When I use the first two args then the below code works fine, with the third args I am getting an error. I have already referred to the new Django 1.5 update for url usage and accordingly used quotes for the URL name.

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

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 file

html文件

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

If I use only two args (i.e city and score, then it works fine) else I get the no reverse match error.

如果我只使用两个args(即城市和得分,那么它工作正常),否则我得到无反向匹配错误。

views.py

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 个解决方案

#1


0  

The distance value 1196.61 does not match the regex because of the decimal point.

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

You can use

您可以使用

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

which matches uppercase A-Z, lowercase a-z, digits 0-9, hyphens and decimal points.

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

Alternatively, you could use

或者,你可以使用

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

Which only matches digits 0-9 and decimal points.

其中只匹配数字0-9和小数点。

#1


0  

The distance value 1196.61 does not match the regex because of the decimal point.

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

You can use

您可以使用

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

which matches uppercase A-Z, lowercase a-z, digits 0-9, hyphens and decimal points.

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

Alternatively, you could use

或者,你可以使用

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

Which only matches digits 0-9 and decimal points.

其中只匹配数字0-9和小数点。