2013-04-05 31 views
0

我想從視圖中傳遞參數以在Django中查看,當我首先傳遞三個參數時,當超過3個參數不再工作時。 同時傳遞參數,我有這樣的錯誤:NoReverseMatch at/detail/Reverse爲''帶參數'()'和關鍵字參數{}未找到

NoReverseMatch at /detail/ 
Reverse for 'display_filter' with arguments '()' and keyword arguments '{'country': 'USA', 'street': 'Wall Street', 'continent': 'America', 'city': 'new York'}' not found. 

urls.py

url(r'^detail/$', 'examples.views.detail'), 
url(r'^display_filter/(?P<continent>[-\w]+)/(?P<country>[-\w]+)/(?P<city>[-\w]+)/(?P<street>[-\w]+)/$', 'examples.views.display_filter', name='display_filter'), 

views.py

def detail(request): 
    continents = Select_continent() 
    if request.method == 'POST': 
     continent = request.POST.get('combox1') 
     country = request.POST.get('combox2') 
     city = request.POST.get('combox3') 
     street = request.POST.get('combox4') 
     countries =Select_country(continent) 
     cities= Select_city(continent,country) 
     streets = Select_street(continent,country,city) 
     for row in continents : 
      if row[0]==int(continent) : 
       param1 =row[1] 
     for row in countries: 
      if row[0]==int(country): 
       param2=row[1]  
     for row in cities: 
      if row[0]==int(city): 
       param3=row[1] 
     for row in streets: 
      if row[0]==int(street): 
       param4=row[1]  
     url = reverse('display_filter', args=(), kwargs={'continent':param1,'country':param2,'city':param3,'street':param4}) 
     return redirect(url) 

    return render(request, 'filter.html', {'items': continents,}) 

def display_filter(request,continent, country,city, street): 

    data = Select_WHERE(continent, country, city,street) 
    #symbol = ConvertSymbol(currency) 
    return render_to_response('filter.html', {'data': data, }, RequestContext(request))  
+0

就可以完成你urls.py碼 – catherine 2013-04-05 17:03:38

回答

1

它看起來就像是你的正則表達式的網址有問題。

你有什麼

(?P<city>[-\w]) 

將只匹配1位,字字符,空格,下劃線或連字符。你應該有什麼是

(?P<city>[-\w]+) 

這將匹配1或更多像你與其他人一樣。


的另一件事是,你可以嘗試在改變

url = reverse('display_filter', args=(), kwargs={'continent':param1,'country':param2,'city':param3,'street':param4}) 
return redirect(url) 

return redirect('display_filter', continent=param1, country=param2, city=param3, street=param4) 

redirect意味着是一條捷徑,所以你不必調用reverse因爲它不爲你做到這一點。

+0

我解決,但沒什麼變化,再次出現同樣的錯誤 – Imoum 2013-04-05 15:30:59

+0

@AmineAntri編輯 – Ngenator 2013-04-05 16:10:06

+0

我認爲這個問題是不如預期, 「{」全國字典沒有下令「:‘突尼斯’ ,'街道':'大道hbib布爾吉巴','大陸':'非洲','城市':'突尼斯'} 但字典中的第一項是大陸而非國家 – Imoum 2013-04-05 16:27:19

0

我認爲你需要做兩件事情:

  1. 添加URL到您的urls.py與您3個PARAMS情況相符:

    url(r'^display_filter/(?P[-\w]+)/(?P[-\w]+)/(?P[-\w]+)/$', 'examples.views.display_filter', name='display_filter'),

  2. 您必須設置您查看方法中第四個參數的默認值:

    def display_filter(request, continent, country, city, street=None):

然後,你可以用三個參數調用URL。

相關問題