我正在使用 Django 1.5 并尝试将 args 传递给我的 URL。当我使用前两个参数时,下面的代码工作正常,第三个参数我得到一个错误。我已经参考了新的 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
网址.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>
如果我只使用两个参数(即城市和分数,那么它工作正常),否则我会得到没有反向匹配错误。
视图.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})