add port from settings file to an url

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def with_port(url_str):
    """ Adds to an URL the port from the settings
        useful for development

        >>> with_port("http://localhost/ddd")
        'http://localhost:8000/ddd'
        >>> with_port("localhost/ddd")
        'localhost/ddd'
    """
    try:
        port = settings.PORT
    except AttributeError:
        port = None
    if port == 80:
        port = None
    url_split = urlsplit(url_str)
    if port:
        if not url_split.port and url_split.netloc:
            scheme, netloc, url, query, fragment = url_split
            netloc += ":%s" % port
            url_split = SplitResult(scheme, netloc, url, query, fragment)
    return url_split.geturl()

More like this

  1. Url persistance of GET variables by alex_ndc 4 years, 6 months ago
  2. Custom filename example with sorl-thumbnail by sajal 2 years, 3 months ago
  3. SelectRelatedManager by realmac 1 year, 8 months ago
  4. Read more link by nny777 4 years, 10 months ago
  5. fancy_if by Scanner 6 years, 1 month ago

Comments

(Forgotten your password?)