error migrating from 4.2

I have updated to django 5.0 from 4.2

I recive the error

 Internal Server Error: /
 Traceback (most recent call last):
   File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
     response = get_response(request)
   File "/usr/local/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__
     response = self.process_request(request)
   File "/usr/local/lib/python3.10/site-packages/django/middleware/security.py", line 28, in process_request
     host = self.redirect_host or request.get_host()
   File "/usr/local/lib/python3.10/site-packages/django/http/request.py", line 140, in get_host
     domain, port = split_domain_port(host)
   File "/usr/local/lib/python3.10/site-packages/django/http/request.py", line 711, in split_domain_port
     domain, port = match.groups(default="")
 ValueError: not enough values to unpack (expected 2, got 0)
 Internal Server Error: /
 Traceback (most recent call last):
   File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
     response = get_response(request)
   File "/usr/local/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__
     response = self.process_request(request)
   File "/usr/local/lib/python3.10/site-packages/django/middleware/security.py", line 28, in process_request
     host = self.redirect_host or request.get_host()
   File "/usr/local/lib/python3.10/site-packages/django/http/request.py", line 140, in get_host
     domain, port = split_domain_port(host)
   File "/usr/local/lib/python3.10/site-packages/django/http/request.py", line 711, in split_domain_port
     domain, port = match.groups(default="")
 ValueError: not enough values to unpack (expected 2, got 0)

We’re going to need more details here.

What generated this error?

  • Is this from accessing a page when running runserver?

    • What is the full runserver command used?

    • What URL were you trying to access?

What does your settings.py file look like?

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of fixing your original post for you.)

The error seems to be happening at http/request.py:710 inside the function split_domain_port() (I checked it out at Django 5.0.3):

The host_validation_re variable contains: re.compile(‘[a-zA-z0-9.:]*’)

If I add the exact definition found on the top of that file before the host_validation_re variable is used it will work as expected:

...

host_validation_re = _lazy_re_compile(
   r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9.:]+\])(?::([0-9]+))?$"
)

if match := host_validation_re.fullmatch(host.lower()):
  ...

I haven’t been able to reproduce the problem in a clean project yet, but I am starting to think it might be a conflict with some libraries.

I have checked that the object id for host_validation_re stays immutable on a clean project while exchanging it in a failing project. This means that some other library or instance is accessing and changing it. It will work as expected if I duplicate the line with a new variable name and rename the variable inside the split_domain_port() function to that name.

The bug is not related to Django 5.0.x

At some point in our project, we had to add in our startup script:

# Disable Django Host Validation System
import django.http.request
django.http.request.host_validation_re = _lazy_re_compile(r"[a-zA-z0-9.:]*")

This is what is causing us so much trouble.

We added this after following the guide:
How to Solve “The domain name provided is not valid according to RFC 1034/1035” in Django during Development

Removing this lines resolved the issue. Probably a similar thing is happening to @brunovila