Django

Code

Ticket #3224: build_url.diff

File build_url.diff, 1.7 kB (added by mihai_preda@yahoo.com, 2 years ago)

refactor: extracted the build_url() function from middleware/common.py and put it into http.

  • http/__init__.py

    old new  
    302302    if not host: 
    303303        host = request.META.get('HTTP_HOST', '') 
    304304    return host 
     305 
     306def build_url(request, path, host=None): 
     307    "Builds an absolute URL with the given path." 
     308    if not host: 
     309        host = get_host(request) 
     310         
     311    if host: 
     312        newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', host, path) 
     313    else: 
     314        newurl = path 
     315         
     316    if request.GET: 
     317        newurl += '?' + request.GET.urlencode() 
     318         
     319    return newurl 
  • middleware/common.py

    old new  
    4444                raise RuntimeError, "You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to %s%s (note the trailing slash), or set APPEND_SLASH=False in your Django settings." % (new_url[0], new_url[1]) 
    4545        if new_url != old_url: 
    4646            # Redirect 
    47             if new_url[0]: 
    48                 newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', new_url[0], new_url[1]) 
    49             else: 
    50                 newurl = new_url[1] 
    51             if request.GET: 
    52                 newurl += '?' + request.GET.urlencode() 
     47            newurl = http.build_url(request, host=new_url[0], path=new_url[1]) 
    5348            return http.HttpResponsePermanentRedirect(newurl) 
    5449 
    5550        return None