Refactor URL confs to Django 2.0's simplified routing syntax
See original GitHub issueDjango 2.0 introduced a more readable syntax for URL confs:
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 path() utility function was added to django.urls and should be used from now on for situations in which no raw strings are necessary for regexp matches, while re_path() will replace django.conf.urls.url() for any regexp expressions, as url() is likely to be eventually deprecated.
Beyond, path() and re_path(), include() was also added to django.urls, though the previous one in django.conf.urls still exists to maintain backwards compatibility.
There are about 349 URL confs using the old syntax for url() - of which very few are false positives of other function calls - in the following 37 source files and one bytecode file:
admin/api/urls.py admin/urls/init.py admin/urls/collections.py admin/urls/pages.py admin/viewsets/model.py api/v2/endpoints.py api/v2/router.py contrib/forms/urls.py contrib/forms/wagtail_hooks.py contrib/redirects/urls.py contrib/redirects/wagtail_hooks.py contrib/search_promotions/admin_urls.py contrib/search_promotions/wagtail_hooks.py contrib/settings/urls.py contrib/settings/wagtail_hooks.py contrib/styleguide/wagtail_hooks.py core/urls.py documents/admin_urls.py documents/urls.py documents/wagtail_hooks.py embeds/urls.py embeds/wagtail_hooks.py images/admin_urls.py images/tests/urls.py images/urls.py images/wagtail_hooks.py project_template/project_name/urls.py search/urls/admin.py search/urls/frontend.py search/wagtail_hooks.py snippets/urls.py snippets/wagtail_hooks.py tests/non_root_urls.py tests/testapp/urls.py tests/urls.py users/urls/users.py users/wagtail_hooks.py api/v2/pycache/router.cpython-36.pyc
I just recently started using Wagtail, and if it’s OK, I’d like to do the refactoring, as I feel it’s beneficial for readability sake, while leaving it be would be jarring for those coming to Wagtail after completing the Django 2.0 tutorial which already makes use of the new syntax.
For all simple URL conf replacements with no regexp involved, I’ll replace url() with path(), while ones with regexp will have their expressions left as is but have url() be replaced by re_path(). The module imports of “from django.conf.urls import include, url” will be replaced with “from django.urls import include, path, re_path”
Note: 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 your URLconfs.
Also, the Wagtail “Getting Started -> Integrating Wagtail into a Django project” tutorial has already started to incorporate some of these changes. Unfortunately, the project template from “wagtail start” still generates a urls.py with the old syntax.
As for replacing the regexps of re_path() with custom path converters, I think that can be addressed later in a larger discussion about trade-offs in a separate issue.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:7 (1 by maintainers)
Top GitHub Comments
Completed in #5844 / #6210.
OK, @zerolab . Thank you!!