Opened 87 minutes ago

Last modified 22 minutes ago

#36923 assigned Uncategorized

URLField mangles mailto: URLs

Reported by: waveywhite Owned by: Vishy Algo
Component: Forms Version: 5.2
Severity: Normal Keywords: mailto urlfield
Cc: waveywhite Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Having derived from django.forms.URLField and django.models.URLField to set up a form that can handle mailto URLs, I'm finding that the URL gets mangled. Entering "mailto:test@…" results in "mailto://test@…".

This was encountered while creating models for a Wagtail site. Wagtail uses Django's models and form fields within its framework.

My modified URLFields:

from django import forms
from django.db import models

class ContactUrlFormField(forms.URLField):
    default_validators = [
        validators.URLValidator(schemes=['http', 'https', 'mailto', 'tel'])
    ]

class ContactUrlField(models.URLField):
    default_validators = [
        validators.URLValidator(schemes=['http', 'https', 'mailto', 'tel'])
    ]
    
    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": ContactUrlFormField,
                **kwargs,
            }
        )

NB. It would be great to have a way of defining which URL schemes are allowed as an option in the model field and then have it picked up by the form field automatically.

Change History (1)

comment:1 by Vishy Algo, 22 minutes ago

Owner: set to Vishy Algo
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top