Django urlfield http prefix

Does anyone know how to get rid of the http: // 'prefix in Django urlfield.

I mean, when we define a field as urlfield and try to enter a URL for it, django will automatically add the http: // prefix to it if no scheme provides. I do not want this prefix.

I am trying to remove it under clean_field and with a clean method. This does not work.

I dig the source code. I saw django add 'http: //' to the 'to_python' method under the UrlField class.

Is there a way to override it to get rid of 'http: //'?

+5
source share
1 answer

Without a schema prefix, a string cannot be a true URL, and URLFieldtherefore will not support it.

URLField CharField URLValidator, , SchemelessURLValidator ( ) CharField, , .

, ,

class SchemelessURLValidator(URLValidator):
    regex = re.compile(
    r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'  # domain...
    r'localhost|'  # localhost...
    r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|'  # ...or ipv4
    r'\[?[A-F0-9]*:[A-F0-9:]+\]?)'  # ...or ipv6
    r'(?::\d+)?'  # optional port
    r'(?:/?|[/?]\S+)$', re.IGNORECASE)
+4

All Articles