(django)url regex不匹配

时间:2017-04-17 10:49:35

标签: regex django django-urls

我尝试的网址:http://127.0.0.1:8000/manifest/archive/?page=1。 正则表达式模式:

  1. (我尝试的原始版本):r'^archive/(?P<reverse>[n]{0,1})/?\?page=[0-9]+/?'
  2. (我也在python控制台中尝试过。)r'^archive/\?page=[0-9]+'
  3. (最容易的,只是为了尝试。)r'^archive/\?page=1'(但我以正确的方式逃过了问号,不是吗?)
  4. 我在python控制台中尝试了所有这些正则表达式并且它们工作得很好,所以我认为问题不在于错误的正则表达式。

    所以:为什么不做这项工作?

    可选信息:

    项目url-conf:

    urlpatterns = [
      url(r'^admin/', admin.site.urls),
      url(r'^$', RedirectView.as_view(url=reverse_lazy('manifest:index'))),
      url(r'^manifest/', include('manifest.urls', namespace='manifest')),
    ]
    

    注意:网址127.0.0.1:8000/manifest/index会匹配,因此无法解决所有问题。

    网址追溯:

    Page not found (404)

    Request Method: GET

    Request URL: http://127.0.0.1:8000/manifest/archive/?page=1

    Using the URLconf defined in asqiir005.urls, Django tried these URL patterns, in this order:

    ^admin/
    ^$
    ^manifest/ ^$ [name='index']
    ^manifest/ ^(?P<number>[0-9]+)$ [name='article']
    ^manifest/ ^archive/\?page=1
    ^manifest/ ^archive/(?P<reverse>[n]{0,1})/?\?page=[0-9]+/? [name='paged_archive']
    ^manifest/ ^archive/all/(?P<reverse>[n]{0,1})/?$ [name='whole_archive']
    ^media\/(?P<path>.*)$
    

    The current URL, manifest/archive/, didn't match any of these.

2 个答案:

答案 0 :(得分:1)

您可以在视图中使用request.GET.get(&#39; page&#39;,&#39;&#39;)。

其他选项:https://stackoverflow.com/a/150518/3336408

答案 1 :(得分:1)

这应该是您的文件 manifest.urls

urlpatterns = [
    url(r'^archive/?$', view_to_handle),
]