为什么要在Django中连接库路径?

时间:2018-09-01 19:11:11

标签: python django

当我在url的应用常规中添加库路径时,向我发送下一条消息:“ E0611:模块'django.urls'中没有名称'path'”和“ E0611:模块'django中没有名称're_path' .urls'”。

这是将军网址中的代码:

from django.contrib import admin
from django.urls import include,re_path
from django.urls import include,path
from homepage.views import homepage

urlpatterns = [
    path(r'^admin/', admin.site.urls),
    path(r'^',include('homepage.urls')),
]

urlpatterns +=[
    re_path(r'home/',include('homepage.urls'))
]

我使用的版本是v2.1

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在官方文档中,他们解释了很酷。您需要进一步学习文档

#Example of using re_path

from django.urls import include, re_path

urlpatterns = [
    re_path(r'^index/$', views.index, name='index'),
    re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'),
    re_path(r'^weblog/', include('blog.urls')),
    ...
] 

 #Example of using path

from django.urls import include, path

urlpatterns = [
    path('index/', views.index, name='main-view'),
    path('bio/<username>/', views.bio, name='bio'),
    path('articles/<slug:title>/', views.article, name='article-detail'),
    path('articles/<slug:title>/<int:section>/', views.section, name='article-section'),
    path('weblog/', include('blog.urls')),
    ...
]

文档:

  1. https://docs.djangoproject.com/en/2.1/ref/urls/#re-path
  2. https://docs.djangoproject.com/en/2.1/ref/urls/#path