From 1fb86d4d93a421ba1382a25c2866efb384d1a674 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 1 Feb 2022 13:48:56 +0100 Subject: [PATCH] Fix django.conf.url deprecation in django 4.0 --- README.md | 9 ++++++++- example_project/example_project/urls.py | 9 ++++++++- setup.py | 2 +- survey/urls.py | 7 ++++++- urls.py | 9 ++++++++- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 76bb0a08..732b2d01 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,14 @@ Add a URL entry to your project’s urls.py, for example: ```python from django.conf import settings -from django.conf.urls import include, url +from django.conf.urls import include + +try: + from django.conf.urls import url +except ImportError: + # Django 4.0 replaced url by something else + # See https://stackoverflow.com/a/70319607/2519059 + from django.urls import re_path as url urlpatterns = [ # Your own url pattern here diff --git a/example_project/example_project/urls.py b/example_project/example_project/urls.py index a6f69427..505e9be6 100644 --- a/example_project/example_project/urls.py +++ b/example_project/example_project/urls.py @@ -13,7 +13,14 @@ 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ -from django.conf.urls import include, url +from django.conf.urls import include + +try: + from django.conf.urls import url +except ImportError: + # Django 4.0 replaced url by something else + # See https://stackoverflow.com/a/70319607/2519059 + from django.urls import re_path as url from django.contrib import admin urlpatterns = [url(r"^admin/", admin.site.urls), url(r"^survey/", include("survey.urls"))] diff --git a/setup.py b/setup.py index 58a976f3..b2581e18 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ LONG_DESCRIPTION = f.read() DEPENDENCIES = [ - "django>=2.2,<4", + "django>=2.2", "django-bootstrap-form>=3.4", "django-tastypie>=0.14.2", "django-registration>=3.0", diff --git a/survey/urls.py b/survey/urls.py index 938a334f..ca968758 100644 --- a/survey/urls.py +++ b/survey/urls.py @@ -1,4 +1,9 @@ -from django.conf.urls import url +try: + from django.conf.urls import url +except ImportError: + # Django 4.0 replaced url by something else + # See https://stackoverflow.com/a/70319607/2519059 + from django.urls import re_path as url from survey.views import ConfirmView, IndexView, SurveyCompleted, SurveyDetail from survey.views.survey_result import serve_result_csv diff --git a/urls.py b/urls.py index 001e2542..2b3b797d 100644 --- a/urls.py +++ b/urls.py @@ -1,6 +1,13 @@ # pylint: disable=invalid-name -from django.conf.urls import include, url +from django.conf.urls import include + +try: + from django.conf.urls import url +except ImportError: + # Django 4.0 replaced url by something else + # See https://stackoverflow.com/a/70319607/2519059 + from django.urls import re_path as url from django.contrib import admin from django.shortcuts import redirect from django.urls.base import reverse