Ticket #8960: 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.diff

File 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.diff, 3.0 KB (added by bmihelac, 7 years ago)
  • django/views/defaults.py

     
    2525        return http.HttpResponseRedirect(absurl)
    2626
    2727    object_domain = None
     28   
     29    # Look for object domain only if contrib.sites is installed
     30    if Site._meta.installed:
     31        # Otherwise, we need to introspect the object's relationships for a
     32        # relation to the Site object
     33        opts = obj._meta
    2834
    29     # Otherwise, we need to introspect the object's relationships for a
    30     # relation to the Site object
    31     opts = obj._meta
    32 
    33     # First, look for an many-to-many relationship to sites
    34     for field in opts.many_to_many:
    35         if field.rel.to is Site:
    36             try:
    37                 object_domain = getattr(obj, field.name).all()[0].domain
    38             except IndexError:
    39                 pass
    40             if object_domain is not None:
    41                 break
    42 
    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:
     35        # First, look for an many-to-many relationship to sites
     36        for field in opts.many_to_many:
     37            if field.rel.to is Site:
    4738                try:
    48                     object_domain = getattr(obj, field.name).domain
    49                 except Site.DoesNotExist:
     39                    object_domain = getattr(obj, field.name).all()[0].domain
     40                except IndexError:
    5041                    pass
    5142                if object_domain is not None:
    5243                    break
    5344
    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
     45        # Next look for a many-to-one relationship to site
     46        if object_domain is None:
     47            for field in obj._meta.fields:
     48                if field.rel and field.rel.to is Site:
     49                    try:
     50                        object_domain = getattr(obj, field.name).domain
     51                    except Site.DoesNotExist:
     52                        pass
     53                    if object_domain is not None:
     54                        break
    6055
     56        # Fall back to the current site (if possible)
     57        if object_domain is None:
     58            try:
     59                object_domain = Site.objects.get_current().domain
     60            except (Site.DoesNotExist):
     61                pass
     62
    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:
    6466        protocol = request.is_secure() and 'https' or 'http'
    6567        return http.HttpResponseRedirect('%s://%s%s' % (protocol, object_domain, absurl))
    6668    else:
     69        print absurl
    6770        return http.HttpResponseRedirect(absurl)
    6871
    6972def page_not_found(request, template_name='404.html'):
Back to Top