为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式没办法用

为什么没办法像教程里一样用正则表达式

  1. from django.contrib import admin
    from django.urls import include, path
    import re
    urlpatterns = [
        path('admin/', admin.site.urls),
        path(r'^blog/', include('blog.urls')),
    ]
  2. urlpatterns = [
        path(r'^index/', views.index),
    ]

可是结果却是:

  1. Page not found (404)Request Method:GET
  2.  Request URL:http://127.0.0.1:8000/blog/index/Using the URLconf defined in MyBlog.urls,
          Django tried these URL patterns, in this order:admin/^blog/The current path, blog/index/, didn't match any of these.You're seeing this error because you have DEBUG = True in
          your Django settings file. Change that to False, and Django
          will display a standard 404 page.

正在回答

3 回答

不需要用正则表达式,视频中不是用的path()。你这个直接写入‘index/’就可以达到正则表达式的效果。

0 回复 有任何疑惑可以回复我~
#1

qq_暮冬_0 提问者

非常感谢!
2018-06-09 回复 有任何疑惑可以回复我~

也是,新版的默认都是path

0 回复 有任何疑惑可以回复我~

Django2.0中path无法匹配正则表达式,如果想跟视频的正则表达式同步,请用下面的方法。

from django.urls import path, re_path

from.import views

urlpatterns = [
    re_path('^index/$',views.index),
]


2 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
django入门与实践
  • 参与学习       65207    人
  • 解答问题       868    个

手把手带你进入Django开发的大门,充分领略Django的魅力

进入课程

正则表达式没办法用

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信