Ticket #9199: patch2.diff

File patch2.diff, 2.9 KB (added by Gonzalo Saavedra, 16 years ago)
  • django/middleware/common.py

     
    5656            try:
    5757                urlresolvers.resolve(request.path_info)
    5858            except urlresolvers.Resolver404:
    59                 new_url[1] = new_url[1] + '/'
    60                 if settings.DEBUG and request.method == 'POST':
    61                     raise RuntimeError, (""
    62                     "You called this URL via POST, but the URL doesn't end "
    63                     "in a slash and you have APPEND_SLASH set. Django can't "
    64                     "redirect to the slash URL while maintaining POST data. "
    65                     "Change your form to point to %s%s (note the trailing "
    66                     "slash), or set APPEND_SLASH=False in your Django "
    67                     "settings.") % (new_url[0], new_url[1])
     59                # Check if slash ending path exists
     60                try:
     61                    urlresolvers.resolve("%s/" % request.path_info)
     62                except urlresolvers.Resolver404:
     63                    pass
     64                else:
     65                    new_url[1] = new_url[1] + '/'
     66                    if settings.DEBUG and request.method == 'POST':
     67                        raise RuntimeError, ("You called this URL via POST, "
     68                        "but the URL doesn't end in a slash and you have "
     69                        "APPEND_SLASH set. Django can't redirect to the slash "
     70                        "URL while maintaining POST data. Change your form to "
     71                        "point to %s%s (note the trailing slash), or set "
     72                        "APPEND_SLASH=False in your Django "
     73                        "settings.") % (new_url[0], new_url[1])
    6874
    6975        if new_url != old_url:
    70             # Redirect if the target url exists
    71             try:
    72                 urlresolvers.resolve("%s/" % request.path_info)
    73             except urlresolvers.Resolver404:
    74                 pass
     76            if new_url[0]:
     77                newurl = "%s://%s%s" % (
     78                    request.is_secure() and 'https' or 'http',
     79                    new_url[0], urlquote(new_url[1]))
    7580            else:
    76                 if new_url[0]:
    77                     newurl = "%s://%s%s" % (
    78                         request.is_secure() and 'https' or 'http',
    79                         new_url[0], urlquote(new_url[1]))
    80                 else:
    81                     newurl = urlquote(new_url[1])
    82                 if request.GET:
    83                     newurl += '?' + request.META['QUERY_STRING']
    84                 return http.HttpResponsePermanentRedirect(newurl)
     81                newurl = urlquote(new_url[1])
     82            if request.GET:
     83                newurl += '?' + request.META['QUERY_STRING']
     84            return http.HttpResponsePermanentRedirect(newurl)
    8585
    8686        return None
    8787
Back to Top