TAGS :Viewed: 11 - Published at: a few seconds ago

[ NoReverseMatch at /detail/ Reverse for '' with arguments '()' and keyword arguments {} not found ]

i want to pass paramters from view to view in Django, when i passed three parameter in the first place, it works but when it is more then 3 paramters does not work anymore. while passing parameters, i have this error :

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

Answer 1


It looks like it is a problem with your regex for the url.

What you have

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

will match only 1 digit, word character, whitespace, underscore, or hyphen. What you should have is

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

which will match 1 or more like you do with the rest of them.


The other thing is that you can try is changing

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

to

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

redirect is meant to be a shortcut so you don't have to call reverse since it does that for you.

Answer 2


I think you need to do two things:

  1. Add a URL to your urls.py that matches with your 3 params case:

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

  2. You must set a default value for the fourth param in your view method:

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

Then, you can call the URL just with three params.