2013-04-05 32 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。

相关问题