Remove autofocus attribute from field in Django

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!

+7
source share
1 answer

(From a comment by @mariodev)

You should be able to do:

 self.fields['username'].widget.attrs.pop("autofocus", None) 

remove the item from the attrs collection if the specified item exists.

+2
source

All Articles