2015-10-17 49 views
1

我想,所以我建我的網址這樣一個整數解析到URL(有3個鏈接,因爲可能沒有參數,地點或場所和一個整數)Django的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和查詢變量得不到任何元素。如果我只是去與第二個網址(例如:「本地主機:8000 /地圖/馬德里」它可以正常工作)

回答

1

你的第二個網址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), 
]