On Django Admin, the default 'create user' form has 3 fields: username, password and confirm password.
I need to customize the create user form. I want to add the firstname
and lastname
fields, and autofill the username field with firstname.lastname
.
How can I do this?
To create a UserForm, click UserForm on the Insert menu in the Visual Basic Editor. Use the Properties window to change the name, behavior, and appearance of the form. For example, to change the caption on a form, set the Caption property.
Step 1 − Navigate to VBA Window by pressing Alt+F11 and Navigate to "Insert" Menu and select "User Form". Upon selecting, the user form is displayed as shown in the following screenshot. Step 2 − Design the forms using the given controls. Step 3 − After adding each control, the controls have to be named.
A UserForm object is a window or dialog box that makes up part of an application's user interface. The UserForms collection is a collection whose elements represent each loaded UserForm in an application. The UserForms collection has a Count property, an Item method, and an Add method.
Something like this should work:
from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User class UserCreateForm(UserCreationForm): class Meta: model = User fields = ('username', 'first_name' , 'last_name', ) class UserAdmin(UserAdmin): add_form = UserCreateForm prepopulated_fields = {'username': ('first_name' , 'last_name', )} add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('first_name', 'last_name', 'username', 'password1', 'password2', ), }), ) # Re-register UserAdmin admin.site.unregister(User) admin.site.register(User, UserAdmin)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With