Django NoReverseMatch at/搜索/

Django NoReverseMatch at/搜索/,django,django-urls,Django,Django Urls,在搜索表单中,用户可以选择一些条件(国家、省和市),并将结果发布到此视图: def profile_search(request): if request.method == 'POST': form = AdvancedSearchForm(request.POST) if form.is_valid(): cd = form.cleaned_data country=cd['country']

在搜索表单中,用户可以选择一些条件(国家、省和市),并将结果发布到此视图:

def profile_search(request):

    if request.method == 'POST': 
        form = AdvancedSearchForm(request.POST)

        if form.is_valid():
            cd = form.cleaned_data

            country=cd['country']
            province=cd['province']
            city = cd['city']

            params=( country, province, city ,)


            url = reverse('userprofile.views.profile_search_result', args= params) 

            return HttpResponseRedirect(url)


    args = {}
    args.update(csrf(request))
    return HttpResponseRedirect("/result/ایران")
应该通过以下url模式之一找到:

url(r'^result/(?P<country>\w+)/(?P<province>\w+)/(?P<city>\w+)', 'userprofile.views.profile_search_result'),

url(r'^result/(?P<country>\w+)/$','userprofile.views.profile_search_result'),
url(r'^result/(?P\w+)/(?P\w+)/(?P\w+),“userprofile.views.profile\u search\u result”,
url(r“^result/(?P\w+/$”,“userprofile.views.profile\u search\u result”),
但是,无论在搜索表单中选择了什么条件,我都会遇到如下错误:

Reverse for 'userprofile.views.profile_search_result' with arguments '(u'\u0627\u06cc\u0631\u0627\u0646', u'\u0627\u0644\u0628\u0631\u0632', u'')' and keyword arguments '{}' not found. 2 pattern(s) tried: ['result/(?P<country>\\w+)/$', 'result/(?P<country>\\w+)/(?P<province>\\w+)/(?P<city>\\w+)']
与“userprofile.views.profile\u search\u result”相反,未找到参数“(u'\u0627\u06cc\u0631\u0627\u0646',u'\u0627\u0644\u0628\u0631\u0632',u')和关键字参数“{}”。尝试了两种模式:[“结果/(?P\\w+)/$”,“结果/(?P\\w+)/(?P\\w+)/(?P\\w+)”]
如何解决这个问题

要使用
reverse()
,您需要为url指定一个名称,并在reverse函数中使用它-请参阅

URL.py中的示例:

url(r'^result/(?P<country>\w+)/(?P<province>\w+)/(?P<city>\w+)', 'userprofile.views.profile_search_result', name='search_results'),

在url.py中尝试这一行

url(r'^result/(?P<country>.*)/$','userprofile.views.profile_search_result'),
你有两个问题

首先,
\w
与您在参数中使用的阿拉伯字符不匹配-它仅相当于
[A-Za-z0-9]
。您需要为要匹配的字符显式使用Unicode码点,或者使用更通用的

r'^result/(?P<country>.+)/(?P<province>.+)/(?P<city>.+)'
r'^result/(?P.+)/(?P.+)/(?P.+)'
其次,在模式中需要city参数,但传递的是空字符串。您可能应该定义忽略该参数的第三个模式:

r'^result/(?P<country>.+)/(?P<province>.+)'
r'^result/(?P.+)/(?P.+)'

但请注意,我不建议这样做。与其将表单作为帖子提交并重定向到具有URL参数的第二个视图来实际执行搜索,不如在表单中使用GET操作并将其直接提交到search_result视图,从
请求中获取搜索查询的位置。get

看起来您传递了4个参数,而不是预期的3个:
带参数'(u062a',u'\u0627\u06cc\u0631\u0627\u0646',u'',u''
。在调用链接到的文档中的
reverse
之前,请尝试打印出
参数,它明确表示您可以直接使用视图(作为字符串路径或可调用)。您不必使用命名URL。使用显式名称当然更整洁,但这不是一项要求。错误消息清楚地表明问题在于参数,而不是名称。正确-不知道这一点。使用此模式问题仍然存在。感谢您的提示Daniel。这解决了我的问题。至于您的建议,我正在尝试使用Post/Redirect/Get是因为有任何原因比直接从request.Get获取数据更糟糕吗?如果直接使用request.Get,我不知道如何清理表单输入。请参阅后续问题:
<a href="{% url "pin-absuser" username %}">
reverse("pin-absuser", args=["وحید"])
r'^result/(?P<country>.+)/(?P<province>.+)/(?P<city>.+)'
r'^result/(?P<country>.+)/(?P<province>.+)'