Ticket #8960: 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.2.diff

File 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.2.diff, 3.1 KB (added by matthijs, 6 years ago)

Updated version

  • django/contrib/contenttypes/views.py

     
    2323    if absurl.startswith('http://') or absurl.startswith('https://'):
    2424        return http.HttpResponseRedirect(absurl)
    2525
    26     # Otherwise, we need to introspect the object's relationships for a
    27     # relation to the Site object
    2826    object_domain = None
    29     opts = obj._meta
    3027
    31     # First, look for an many-to-many relationship to Site.
    32     for field in opts.many_to_many:
    33         if field.rel.to is Site:
    34             try:
    35                 # Caveat: In the case of multiple related Sites, this just
    36                 # selects the *first* one, which is arbitrary.
    37                 object_domain = getattr(obj, field.name).all()[0].domain
    38             except IndexError:
    39                 pass
    40             if object_domain is not None:
    41                 break
     28    # Look for object domain only if contrib.sites is installed
     29    if Site._meta.installed:
     30        # Otherwise, we need to introspect the object's relationships for a
     31        # relation to the Site object
     32        opts = obj._meta
    4233
    43     # Next, look for a many-to-one relationship to Site.
    44     if object_domain is None:
    45         for field in obj._meta.fields:
    46             if field.rel and field.rel.to is Site:
     34        # First, look for an many-to-many relationship to Site.
     35        for field in opts.many_to_many:
     36            if field.rel.to is Site:
    4737                try:
    48                     object_domain = getattr(obj, field.name).domain
    49                 except Site.DoesNotExist:
     38                    # Caveat: In the case of multiple related Sites, this just
     39                    # selects the *first* one, which is arbitrary.
     40                    object_domain = getattr(obj, field.name).all()[0].domain
     41                except IndexError:
    5042                    pass
    5143                if object_domain is not None:
    5244                    break
    5345
    54     # Fall back to the current site (if possible).
    55     if object_domain is None:
    56         try:
    57             object_domain = Site.objects.get_current().domain
    58         except Site.DoesNotExist:
    59             pass
     46        # Next, look for a many-to-one relationship to Site.
     47        if object_domain is None:
     48            for field in obj._meta.fields:
     49                if field.rel and field.rel.to is Site:
     50                    try:
     51                        object_domain = getattr(obj, field.name).domain
     52                    except Site.DoesNotExist:
     53                        pass
     54                    if object_domain is not None:
     55                        break
    6056
     57        # Fall back to the current site (if possible).
     58        if object_domain is None:
     59            try:
     60                object_domain = Site.objects.get_current().domain
     61            except Site.DoesNotExist:
     62                pass
    6163    # If all that malarkey found an object domain, use it. Otherwise, fall back
    6264    # to whatever get_absolute_url() returned.
    6365    if object_domain is not None:
Back to Top