Is it better to use path() or url() in urls.py for django 2.0?

后端 未结 6 1702
小蘑菇
小蘑菇 2020-11-28 02:24

In a django online course, the instructor has us use the url() function to call views and utilize regular expressions in the urlpatterns list. I\'ve seen other

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 03:10

    The new django.urls.path() function allows a simpler, more readable URL routing syntax. For example, this example from previous Django releases:

    url(r'^articles/(?P[0-9]{4})/$', views.year_archive)
    

    could be written as:

    path('articles//', views.year_archive)
    

    The django.conf.urls.url() function from previous versions is now available as django.urls.re_path(). The old location remains for backwards compatibility, without an imminent deprecation. The old django.conf.urls.include() function is now importable from django.urls so you can use:

    from django.urls import include, path, re_path
    

    in the URLconfs. For further reading django doc

提交回复
热议问题