I am working on a registration form, I have several user fields in front of the username. What happens is that, by default, the focus is in the username field, and I cannot remove the autofocus attribute from this field.
I know that I can work using JavaScript, but I try to do it right on Django.
from django import forms from django.contrib.auth.models import User from project.userprofile.models import UserProfile class UserSignupForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(UserSignupForm, self).__init__(*args, **kwargs) self.fields['username'].widget.attrs['autofocus'] = 'off'
Did I miss something?
UPDATE
Decision:
self.fields['username'].widget.attrs.pop("autofocus", None)
Thanks @mariodev!
user2067956
source share