question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor URL confs to Django 2.0's simplified routing syntax

See original GitHub issue

Django 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:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
gasmancommented, Jul 6, 2020

Completed in #5844 / #6210.

1reaction
jotaemeicommented, Oct 27, 2018

OK, @zerolab . Thank you!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

2020-June.txt - Python mailing list
Is there an answer there about how to link particular (versions) Python ... syntax highlighting, syntax analysis, code analysis, refactor, ...
Read more >
Galaxy Code Documentation - Read the Docs
Returns a dictionary containing the id, name, and 'show' url of the ... Returns list of dictionaries containing summary history information.
Read more >
Legacy archives - XML.com
Bob DuCharme shows how an XSLT stylesheet can read simplified XML-conversion instructions and create a new, working XSLT stylesheet from those instructions.
Read more >
believer - OSCHINA - 中文开源技术交流社区
Horizon 是一个使用 Django 开发的项目,旨在为 Openstack 提供一个功能全面的操作面板,包括一个可扩展的框架用于构造新的可重用面板。
Read more >
GeoNode Documentation - Read the Docs - Yumpu
Users: Are people who log into a GeoNode website and use its functionality.2. Administrators: Are people who install and deploy GeoNode ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found