summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorCoen van der Kamp <coen@fourdigits.nl>2023-03-08 20:12:34 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-04-28 06:58:10 +0200
commit7bbbadc69383f0a2b99253e153b974f8783e876d (patch)
treef0e4e8a38904aa9772fdae066dbda9dfd41d08b6 /django
parent070cbac0dbf6a09b55aad322137ab168b75bf56b (diff)
downloaddjango-7bbbadc69383f0a2b99253e153b974f8783e876d.tar.gz
Fixed #34380 -- Allowed specifying a default URL scheme in forms.URLField.
This also deprecates "http" as the default scheme.
Diffstat (limited to 'django')
-rw-r--r--django/forms/fields.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 0143296533..d759da71ab 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -10,6 +10,7 @@ import operator
import os
import re
import uuid
+import warnings
from decimal import Decimal, DecimalException
from io import BytesIO
from urllib.parse import urlsplit, urlunsplit
@@ -42,6 +43,7 @@ from django.forms.widgets import (
)
from django.utils import formats
from django.utils.dateparse import parse_datetime, parse_duration
+from django.utils.deprecation import RemovedInDjango60Warning
from django.utils.duration import duration_string
from django.utils.ipv6 import clean_ipv6_address
from django.utils.regex_helper import _lazy_re_compile
@@ -753,7 +755,19 @@ class URLField(CharField):
}
default_validators = [validators.URLValidator()]
- def __init__(self, **kwargs):
+ def __init__(self, *, assume_scheme=None, **kwargs):
+ if assume_scheme is None:
+ warnings.warn(
+ "The default scheme will be changed from 'http' to 'https' in Django "
+ "6.0. Pass the forms.URLField.assume_scheme argument to silence this "
+ "warning.",
+ RemovedInDjango60Warning,
+ stacklevel=2,
+ )
+ assume_scheme = "http"
+ # RemovedInDjango60Warning: When the deprecation ends, replace with:
+ # self.assume_scheme = assume_scheme or "https"
+ self.assume_scheme = assume_scheme
super().__init__(strip=True, **kwargs)
def to_python(self, value):
@@ -773,8 +787,8 @@ class URLField(CharField):
if value:
url_fields = split_url(value)
if not url_fields[0]:
- # If no URL scheme given, assume http://
- url_fields[0] = "http"
+ # If no URL scheme given, add a scheme.
+ url_fields[0] = self.assume_scheme
if not url_fields[1]:
# Assume that if no domain is provided, that the path segment
# contains the domain.