Custom labels - Django auth user model - ModelForm

Viewed 61

I'm trying to set custom labels for a UserCreationForm in Django. I've tried to follow the docs and searched on here. Only the first label changes via the labels dictionary I created. org.label at the top has been tested just to see if changes are made by setting a label outside of the dictionary.

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _

class SignUpForm(UserCreationForm):
    email = forms.EmailField(required=True)
    org = forms.CharField(required=True)
    org.label = 'test org label'
    
    class Meta:
        model = User
        fields = ["username", "email", "org", "password1", "password2"]
        labels = {
            "username":_("xx-Username"),
            "email":_("xx-Email"),
            "org": _("xx-Organisation"),
            "password1":_("xx-Password"),
            "password2":"xx-Confirm Password"
            }

The output I get is linked as an image. https://i.stack.imgur.com/7kGUY.png

The placeholder outputs are defined by the field.label variable. I've used django-widget-tweaks to output placeholder=field.label.

Many thanks.

1 Answers

I solved it by using

def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['password2'].label = 'Confirm Password'

within the SignUpForm class.

Related