Django NoReverseMatch 在 /search/
Django NoReverseMatch at /search/
在搜索表单中,用户可以选择一些条件(国家、省份和城市),post将结果显示到此视图:
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'),
但是,无论在搜索表单中选择什么条件,我都会收到如下错误:
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+)']
如何解决这个问题?
要使用 reverse()
,您需要为 url 命名,并在反向函数中使用它 - 参见 documentation。
urls.py中的示例:
url(r'^result/(?P<country>\w+)/(?P<province>\w+)/(?P<city>\w+)', 'userprofile.views.profile_search_result', name='search_results'),
调用示例:
url = reverse('search_results', args= params)
在 urls.py
中试试这一行
url(r'^result/(?P<country>.*)/$','userprofile.views.profile_search_result'),
这是在我的应用程序中使用 unicode 字符的示例:
urls.py
urlpatterns += patterns('pin.views',
url(r'^(?P<user_namefl>.*)/followers/$', 'absuser_followers', name='pin-absuser-followers'),
url(r'^(?P<user_namefg>.*)/following/$', 'absuser_friends', name='pin-absuser-following'),
url(r'^(?P<user_namel>.*)/likes/$', 'absuser_like', name='pin-absuser-like'),
url(r'^(?P<user_name>.*)/$', 'absuser', name='pin-absuser'),
)
来自 html 页的链接
<a href="{% url "pin-absuser" username %}">
在视图或模型中有反向
reverse("pin-absuser", args=["وحید"])
你有两个问题。
首先,\w
与您在参数中使用的阿拉伯字符不匹配 - 它只相当于 [A-Za-z0-9]
。您需要为要匹配的字符显式使用 Unicode 代码点,或者更通用的 .
:
r'^result/(?P<country>.+)/(?P<province>.+)/(?P<city>.+)'
其次,在您的模式中,城市参数是必需的,但您传递的是一个空字符串。您可能应该定义省略该参数的第三个模式:
r'^result/(?P<country>.+)/(?P<province>.+)'
但请注意,我不建议您这样做。与其将表单作为 POST 提交并重定向到带有 URL 参数的第二个视图来实际进行搜索,不如在表单中使用 GET 操作并将其直接提交到 search_result 视图,您可以在其中从 request.GET
.
获取搜索查询
在搜索表单中,用户可以选择一些条件(国家、省份和城市),post将结果显示到此视图:
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'),
但是,无论在搜索表单中选择什么条件,我都会收到如下错误:
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+)']
如何解决这个问题?
要使用 reverse()
,您需要为 url 命名,并在反向函数中使用它 - 参见 documentation。
urls.py中的示例:
url(r'^result/(?P<country>\w+)/(?P<province>\w+)/(?P<city>\w+)', 'userprofile.views.profile_search_result', name='search_results'),
调用示例:
url = reverse('search_results', args= params)
在 urls.py
中试试这一行url(r'^result/(?P<country>.*)/$','userprofile.views.profile_search_result'),
这是在我的应用程序中使用 unicode 字符的示例:
urls.py
urlpatterns += patterns('pin.views',
url(r'^(?P<user_namefl>.*)/followers/$', 'absuser_followers', name='pin-absuser-followers'),
url(r'^(?P<user_namefg>.*)/following/$', 'absuser_friends', name='pin-absuser-following'),
url(r'^(?P<user_namel>.*)/likes/$', 'absuser_like', name='pin-absuser-like'),
url(r'^(?P<user_name>.*)/$', 'absuser', name='pin-absuser'),
)
来自 html 页的链接
<a href="{% url "pin-absuser" username %}">
在视图或模型中有反向
reverse("pin-absuser", args=["وحید"])
你有两个问题。
首先,\w
与您在参数中使用的阿拉伯字符不匹配 - 它只相当于 [A-Za-z0-9]
。您需要为要匹配的字符显式使用 Unicode 代码点,或者更通用的 .
:
r'^result/(?P<country>.+)/(?P<province>.+)/(?P<city>.+)'
其次,在您的模式中,城市参数是必需的,但您传递的是一个空字符串。您可能应该定义省略该参数的第三个模式:
r'^result/(?P<country>.+)/(?P<province>.+)'
但请注意,我不建议您这样做。与其将表单作为 POST 提交并重定向到带有 URL 参数的第二个视图来实际进行搜索,不如在表单中使用 GET 操作并将其直接提交到 search_result 视图,您可以在其中从 request.GET
.