0

我想在 Django 中将参数从视图传递到视图,当我首先传递三个参数时,它可以工作,但是当它超过 3 个参数时不再工作。在传递参数时,我有这个错误:

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

网址.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'),

视图.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))     
4

2 回答 2

1

看起来您的 url 正则表达式有问题。

你有什么

(?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,因为它会为你做。

于 2013-04-05T15:18:48.560 回答
0

我认为你需要做两件事:

  1. 向您的 urls.py 添加一个与您的 3 参数案例匹配的 URL:

    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。

于 2013-04-05T17:08:03.837 回答