77from django .contrib .auth .models import Group , User
88from django .contrib .sites .models import Site
99from django .core .mail import send_mail
10+ from django .db .models import URLField
1011from django .forms .widgets import NumberInput
1112from django .template import loader
1213
@@ -32,6 +33,16 @@ def clean(self):
3233 return self .cleaned_data
3334
3435
36+ # Assume all URLField will be HTTPS if not specified.
37+ # NOTE: this can be removed once we bump Django to 6.x
38+ # where `https` becomes the default. Since we are using
39+ # form.ModelForm, we do this to apply https to all UrlField.
40+ def _urlfields_assume_https (db_field , ** kwargs ):
41+ if isinstance (db_field , URLField ):
42+ kwargs ["assume_scheme" ] = "http"
43+ return db_field .formfield (** kwargs )
44+
45+
3546class UserProfileForm (forms .ModelForm ):
3647 def clean_pgp_key (self ):
3748 data = self .cleaned_data ['pgp_key' ]
@@ -46,6 +57,7 @@ class Meta:
4657 widgets = {
4758 'yob' : NumberInput (attrs = {'min' : 1950 , 'max' : date .today ().year - 10 }),
4859 }
60+ formfield_callback = _urlfields_assume_https
4961
5062
5163class NewUserForm (forms .ModelForm ):
@@ -58,6 +70,7 @@ class NewUserForm(forms.ModelForm):
5870 class Meta :
5971 model = UserProfile
6072 exclude = ('picture' , 'user' )
73+ formfield_callback = _urlfields_assume_https
6174
6275 def __init__ (self , * args , ** kwargs ):
6376 super (NewUserForm , self ).__init__ (* args , ** kwargs )
0 commit comments