django 2.1.4 遇到的一些问题

url 被 path 和 re_path 取代

没有作深入研究,现在通俗的理解就是, 又测试了一下,前面说的不正确

  • path 用于不支持正则匹配,是直接判断字符串是否相等的
  • re_path 可以用 正则匹配

在 path 中命名 namespace 出错

django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

就是说,在项目的 urls 中 使用 include 时, 添加了namespace 参数,那么在相应的被包含模块中也要定义 对应的 app_name
修改后正确,代码如下
项目 urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('book/', include('bookinfo.urls', namespace='book')),
]

对应的模块urls中:

app_name = 'book'
urlpatterns = [
    re_path(r'^index/$', views.index, name='index'),
]
posted @ 2019-07-08 17:48  月夜鸟525  阅读(225)  评论(0编辑  收藏  举报