gpt4 book ai didi

django - 无反向匹配 : with arguments '()' and keyword arguments

转载 作者:行者123 更新时间:2023-12-02 07:12:19 31 4
gpt4 key购买 nike

我正在使用 Django 1.5 并尝试将参数传递到我的 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

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

最佳答案

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

你可以使用

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

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

或者,您可以使用

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

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

关于django - 无反向匹配 : with arguments '()' and keyword arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18069808/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com