summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
Diffstat (limited to 'django')
-rw-r--r--django/test/client.py7
-rw-r--r--django/utils/http.py23
2 files changed, 23 insertions, 7 deletions
diff --git a/django/test/client.py b/django/test/client.py
index d14ba43792..07641b7e65 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -192,7 +192,12 @@ def encode_multipart(boundary, data):
# file, or a *list* of form values and/or files. Remember that HTTP field
# names can be duplicated!
for (key, value) in data.items():
- if is_file(value):
+ if value is None:
+ raise TypeError(
+ 'Cannot encode None as POST data. Did you mean to pass an '
+ 'empty string or omit the value?'
+ )
+ elif is_file(value):
lines.extend(encode_file(boundary, key, value))
elif not isinstance(value, str) and is_iterable(value):
for item in value:
diff --git a/django/utils/http.py b/django/utils/http.py
index db18e57803..de1ea71368 100644
--- a/django/utils/http.py
+++ b/django/utils/http.py
@@ -91,20 +91,31 @@ def urlencode(query, doseq=False):
query = query.items()
query_params = []
for key, value in query:
- if isinstance(value, (str, bytes)):
+ if value is None:
+ raise TypeError(
+ 'Cannot encode None in a query string. Did you mean to pass '
+ 'an empty string or omit the value?'
+ )
+ elif isinstance(value, (str, bytes)):
query_val = value
else:
try:
- iter(value)
+ itr = iter(value)
except TypeError:
query_val = value
else:
# Consume generators and iterators, even when doseq=True, to
# work around https://bugs.python.org/issue31706.
- query_val = [
- item if isinstance(item, bytes) else str(item)
- for item in value
- ]
+ query_val = []
+ for item in itr:
+ if item is None:
+ raise TypeError(
+ 'Cannot encode None in a query string. Did you '
+ 'mean to pass an empty string or omit the value?'
+ )
+ elif not isinstance(item, bytes):
+ item = str(item)
+ query_val.append(item)
query_params.append((key, query_val))
return original_urlencode(query_params, doseq)