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

In the django online course, the teacher uses the url() function to invoke views and use regular expressions in the urlpatterns list. I saw other examples on youtube of this. eg.

 from django.contrib import admin from django.urls import include from django.conf.urls import url urlpatterns = [ path('admin/', admin.site.urls), url(r'^polls/', include('polls.urls')), ] #and in polls/urls.py urlpatterns = [ url(r'^$', views.index, name="index"), ] 

However, when viewing a Django tutorial, they use path() , not eg:

 from django.urls import path from . import views urlpatterns = [ path('', views.index, name="index"), ] 

Also, regular expressions don't seem to work with the path() function, since using path(r'^$', views.index, name="index") will not find the mysite.com/polls/ .

Uses path() without a regex, suitable for further advancement? Is url() more powerful but more complex, so they use path() to start with us? Or is it the case of various tools for different tasks?

+88
source share
6 answers

From Django documentation for URL

url(regex, view, kwargs=None, name=None) This function is an alias of django.urls.re_path() . Most likely, this is not recommended in a future release.

The key difference between path and re_path is that path uses a route without regular expressions

You can use re_path for complex re_path calls and use just path for simpler searches

+114
source

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

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

can be written as:

 path('articles/<int:year>/', 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 backward compatibility, without inevitable wear. The old django.conf.urls.include() now imported from django.urls so you can use:

 from django.urls import include, path, re_path 

in URLconfs . For further reading Django Doc

+41
source

path is just new in Django 2.0, which was only released a couple of weeks ago. Most tutorials will not be updated for the new syntax.

Of course, this was an easier way to do things; I would not say that the URL is more powerful, but you should be able to express patterns in any format.

+16
source

Path is a new feature of Django 2.0. Explained here: https://docs.djangoproject.com/en/2.0/releases/2.0/#whats-new-2-0

It looks like a more pythonic way and allows you not to use the regular expression in the argument that you pass for viewing ... you can use the int () function as an example.

+6
source

Regular expressions do not seem to work with the path() function with the following arguments: path(r'^$', views.index, name="index") .

It should be like this: path('', views.index, name="index") .

The first argument must be empty to introduce a regular expression.

+5
source

Starting with version 2.0, many users use the path, but we can use either the path or the URL. For example, in django 2.1.1, mapping to functions via 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 the application and post_home is the function in views.py

0
source

Source: https://habr.com/ru/post/1274285/


All Articles