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

后端 未结 6 1704
小蘑菇
小蘑菇 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 02:49

    From v2.0 many users are using path, but we can use either path or url. For example in django 2.1.1 mapping to functions through url can be done as follows

    from django.contrib import admin
    from django.urls import path
    
    from django.contrib.auth import login
    from posts.views import post_home
    from django.conf.urls import url
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        url(r'^posts/$', post_home, name='post_home'),
    
    ]
    

    where posts is an application & post_home is a function in views.py

提交回复
热议问题