2013-08-06 164 views
4

我正在使用Django 1.5并尝试将args传递给我的URL。当我使用前两个参数时,下面的代码工作正常,第三个参数出现错误。我已经提到了用于url用法的新Django 1.5更新,并相应地使用了URL名称的引号。NoReverseMatch:带参数'()'和关键字参数

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> 

如果我只使用有两个参数(即城市和分数,然后正常工作),否则我得到没有反向匹配错误。

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}) 
+0

你能发表你的看法吗? –

+0

我认为regx存在一些问题,如果我将“分数”传递给所有参数,那么我不会收到此错误。 – Mayank

+0

我想'data.time'不符合'\ w +'格式。仔细检查一下。 –

回答

0

的距离值1196.61不匹配,因为小数点的正则表达式。

您可以使用

(?P<distance>[\w\.]+) 
它匹配大写字母A-Z

,小写a-z,数字0-9,连字符和小数点。

或者,你可以使用

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

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

+0

谢谢,这真的有帮助! – Mayank

相关问题