summaryrefslogtreecommitdiff
path: root/django/contrib/sites/checks.py
blob: bf7c9b7dce3c4d8582c5e044f540206894b1956c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django.conf import settings
from django.core.checks import Error
from django.core.exceptions import ValidationError


def check_site_id(app_configs, **kwargs):
    # Inner import avoids AppRegistryNotReady
    from django.contrib.sites.models import Site

    if hasattr(settings, "SITE_ID"):
        try:
            site_id = Site._meta.pk.to_python(settings.SITE_ID)
        except ValidationError as exc:
            return [
                Error(
                    f"The SITE_ID setting failed to validate: {exc}.", id="sites.E101"
                ),
            ]
        else:
            # to_python() might coerce a SITE_ID of the wrong type to the valid
            # type, e.g. "1" to 1 for AutoField.
            if site_id != settings.SITE_ID:
                expected_type = type(site_id).__name__
                return [
                    Error(
                        f"The SITE_ID setting must be of type {expected_type}.",
                        id="sites.E101",
                    ),
                ]
    return []