我想将一个整数解析为url,所以我构建了这样的url(有3个url因为没有参数,地点或地方和整数)
urlpatterns = [
url(r'^map/$', geov.map_view, name = "map_view"),
url(r'^map/(?P<search_place>[^\.]+)/$', geov.map_view, name = "map_view_accurate"),
url(r'^map/(?P<search_place>[^\.]+)/(?P<digit>\d+)/$', geov.map_view),
]
所以在我看来,我有:
def map_view(request, search_place = None, digit = 0):
results = {}
print(digit)
if search_place is not None:
query = Place_search.objects.filter(name_lower__icontains=search_place.lower()).order_by("-importance")
results["query"] = query
else:
points = Place.objects.all()
lon = points.count()
results["points"]=points
results["lon"] = lon
return render_to_response("index.html", results)
但是数字总是0而查询变量没有元素。如果我只使用第二个URL(例如:“localhost:8000 / map / madrid”它可以正常工作)
答案 0 :(得分:1)
你的第二个url regexp捕获你想要通过第三个网址捕获的内容,即搜索带有斜线和数字的地方,你应该将它改为:
urlpatterns = [
url(r'^map/$', geov.map_view, name = "map_view"),
url(r'^map/(?P<search_place>[\w-]+)/$', geov.map_view, name = "map_view_accurate"),
url(r'^map/(?P<search_place>[\w-]+)/(?P<digit>\d+)/$', geov.map_view),
]