216👍
From Django documentation for url
url(regex, view, kwargs=None, name=None)
This function
is an alias todjango.urls.re_path()
. It’s likely to be deprecated in
a future release.
Key difference between path
and re_path
is that path
uses route without regex
You can use re_path
for complex regex calls and use just path
for simpler lookups
95👍
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<year>[0-9]{4})/$', views.year_archive)
could 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 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
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-How to 'bulk update' with Django?
- [Django]-How do I filter query objects by date range in Django?
21👍
path
is simply new in Django 2.0, which was only released a couple of weeks ago. Most tutorials won’t have been updated for the new syntax.
It was certainly supposed to be a simpler way of doing things; I wouldn’t say that URL is more powerful though, you should be able to express patterns in either format.
- [Django]-Django – No module named _sqlite3
- [Django]-CSS styling in Django forms
- [Django]-How to remove all of the data in a table using Django
10👍
Regular expressions don’t 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 1st argument must be blank to enter a regular expression.
- [Django]-What is the most efficient way to store a list in the Django models?
- [Django]-Get Timezone from City in Python/Django
- [Django]-Django REST Framework – 405 METHOD NOT ALLOWED using SimpleRouter
9👍
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
Look like more pythonic way, and enable to not use regular expression in argument you pass to view… you can ue int() function for exemple.
- [Django]-How to pass django rest framework response to html?
- [Django]-How do I make many-to-many field optional in Django?
- [Django]-Django urlsafe base64 decoding with decryption
5👍
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
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
- [Django]-How can I keep test data after Django tests complete?
- [Django]-Django South – table already exists